1,完成实体类包
public class Music { private String title; private String singer; private String album; private String url; private long size; private long time; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } }2,完成适配器包,部分代码
public class MusicAdapter extends BaseAdapter {
private List<Music> listMusic; private Context context; public MusicAdapter(Context context,List<Music> listMusic){ this.context=context; this.listMusic=listMusic; } public void setListItem(List<Music> listMusic){ this.listMusic=listMusic; } @Override public int getCount() { // TODO Auto-generated method stub return listMusic.size(); }@Override
public Object getItem(int arg0) { // TODO Auto-generated method stub return listMusic.get(arg0); }@Override
public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; Music m=listMusic.get(position); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.music_item, null); holder = new ViewHolder(); //音乐名 holder.textMusicName = (TextView) convertView.findViewById(R.id.music_item_name); //歌手 holder.textMusicSinger=(TextView) convertView.findViewById(R.id.music_item_singer); //持续时间 holder.textMusicSinger=(TextView) convertView.findViewById(R.id.music_item_time); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textMusicName.setText(m.getName()); holder.textMusicSinger.setText(m.getSinger()); holder.textMusicTime.setText(toTime((int)m.getTime())); return convertView; }static class ViewHolder {
TextView textMusicName; TextView textMusicSinger; TextView textMusicTime; } /** * 时间格式转换 * @param time * */ public String toTime(int time) { time /= 1000; int minute = time / 60; int hour = minute / 60; int second = time % 60; minute %= 60; return String.format("%02d:%02d", minute, second); } }3,完成方法类包
public class MusicList {
public static List<Music> getMusicData(Context context) {
List<Music> musicList = new ArrayList<Music>(); ContentResolver cr = context.getContentResolver(); if (cr != null) { // 获取所有歌曲Cursor cursor = cr.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (null == cursor) { return null; } if (cursor.moveToFirst()) { do { Music m = new Music(); String title = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.TITLE)); String singer = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ARTIST)); if ("<unknown>".equals(singer)) { singer = "未知艺术家"; } String album = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ALBUM)); long size = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.SIZE)); long time = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.DURATION)); String url = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)); String name = cursor .getString(cursor .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); String sbr = name.substring(name.length() - 3, name.length()); //Log.e("--------------", sbr); if (sbr.equals("mp3")) { m.setTitle(title); m.setSinger(singer); m.setAlbum(album); m.setSize(size); m.setTime(time); m.setUrl(url); m.setName(name); musicList.add(m); } } while (cursor.moveToNext()); } } return musicList;}
}4,完成activity
public class MusicActivity extends Activity implements SensorEventListener{
private TextView textName;
private TextView textSinger; private TextView textStartTime; private TextView textEndTime; private ImageButton imageBtnLast; private ImageButton imageBtnRewind; public static ImageButton imageBtnPlay; private ImageButton imageBtnForward; private ImageButton imageBtnNext; private ImageButton imageBtnLoop; private ImageButton imageBtnRandom; public static LrcView lrc_view; private ImageView icon; private SeekBar seekBar1; private AudioManager audioManager;// 音量管理者 private int maxVolume;// 最大音量 private int currentVolume;// 当前音量 private SeekBar seekBarVolume; private List<Music> lists; private Boolean isPlaying = false; private static int id = 1; private static int currentId = 2; private static Boolean replaying=false; private MyProgressBroadCastReceiver receiver; private MyCompletionListner completionListner; public static Boolean isLoop=true; private SensorManager sensorManager; private boolean mRegisteredSensor;@Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.music);textName = (TextView) this.findViewById(R.id.music_name);
textSinger = (TextView) this.findViewById(R.id.music_singer); textStartTime = (TextView) this.findViewById(R.id.music_start_time); textEndTime = (TextView) this.findViewById(R.id.music_end_time); seekBar1 = (SeekBar) this.findViewById(R.id.music_seekBar); //icon = (ImageView) this.findViewById(R.id.image_icon); imageBtnLast = (ImageButton) this.findViewById(R.id.music_lasted); imageBtnRewind = (ImageButton) this.findViewById(R.id.music_rewind); imageBtnPlay = (ImageButton) this.findViewById(R.id.music_play); imageBtnForward = (ImageButton) this.findViewById(R.id.music_foward); imageBtnNext = (ImageButton) this.findViewById(R.id.music_next); imageBtnLoop = (ImageButton) this.findViewById(R.id.music_loop); seekBarVolume = (SeekBar) this.findViewById(R.id.music_volume); imageBtnRandom = (ImageButton) this.findViewById(R.id.music_random); lrc_view = (LrcView) findViewById(R.id.LyricShow); imageBtnLast.setOnClickListener(new MyListener()); imageBtnRewind.setOnClickListener(new MyListener()); imageBtnPlay.setOnClickListener(new MyListener()); imageBtnForward.setOnClickListener(new MyListener()); imageBtnNext.setOnClickListener(new MyListener()); imageBtnLoop.setOnClickListener(new MyListener()); imageBtnRandom.setOnClickListener(new MyListener()); sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);lists = MusicList.getMusicData(this);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 获得最大音量 currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);// 获得当前音量 seekBarVolume.setMax(maxVolume); seekBarVolume.setProgress(currentVolume); seekBarVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Override
public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_ALLOW_RINGER_MODES); } }); //电话状态监听 TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); telManager.listen(new MobliePhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub seekBar1.setProgress(seekBar.getProgress()); Intent intent=new Intent("cn.com.karl.seekBar"); intent.putExtra("seekBarPosition", seekBar.getProgress()); //System.out.println("==========="+seekBar.getProgress()); sendBroadcast(intent); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }); completionListner=new MyCompletionListner(); IntentFilter filter=new IntentFilter("cn.com.karl.completion"); this.registerReceiver(completionListner, filter); } private class MobliePhoneStateListener extends PhoneStateListener {@Override
public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */ Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "playing"); intent.putExtra("id", id); startService(intent); isPlaying = true; imageBtnPlay.setImageResource(R.drawable.pause1); replaying=true; break; case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */ case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */ Intent intent2 = new Intent(MusicActivity.this, MusicService.class); intent2.putExtra("play", "pause"); startService(intent2); isPlaying = false; imageBtnPlay.setImageResource(R.drawable.play1); replaying=false; break; default: break;}
}
}
@Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); receiver=new MyProgressBroadCastReceiver(); IntentFilter filter=new IntentFilter("cn.com.karl.progress"); this.registerReceiver(receiver, filter); id = getIntent().getIntExtra("id", 1); if (id == currentId) { Music m = lists.get(id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "replaying"); intent.putExtra("id", id); startService(intent); if (replaying == true) { imageBtnPlay.setImageResource(R.drawable.pause1); ///replaying=false; isPlaying = true; } else { imageBtnPlay.setImageResource(R.drawable.play1); //replaying=true; isPlaying=false; } } else { Music m = lists.get(id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "play"); intent.putExtra("id", id); startService(intent); isPlaying = true; replaying=true; currentId = id; } } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if(sensors.size()>0){ Sensor sensor=sensors.get(0); mRegisteredSensor=sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST); Log.e("--------------", sensor.getName()); } } @Override protected void onPause() { // TODO Auto-generated method stub if(mRegisteredSensor){ sensorManager.unregisterListener(this); mRegisteredSensor=false; } super.onPause(); } @Override protected void onDestroy() { // TODO Auto-generated method stub this.unregisterReceiver(receiver); this.unregisterReceiver(completionListner); super.onDestroy(); } public class MyProgressBroadCastReceiver extends BroadcastReceiver{@Override
public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub int position=intent.getIntExtra("position", 0); int total=intent.getIntExtra("total", 0); int progress = position * 100 / total; textStartTime.setText(toTime(position)); seekBar1.setProgress(progress); seekBar1.invalidate(); } } private class MyListener implements OnClickListener {@Override
public void onClick(View v) { // TODO Auto-generated method stub if (v == imageBtnLast) { // 第一首 id = 0; Music m = lists.get(0); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "first"); intent.putExtra("id", id); startService(intent); isPlaying = true; } else if (v == imageBtnRewind) { // 前一首 int id=MusicService._id-1; if(id>=lists.size()-1){ id=lists.size()-1; }else if(id<=0){ id=0; } Music m = lists.get(id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "rewind"); intent.putExtra("id", id); startService(intent); isPlaying = true; } else if (v == imageBtnPlay) { // 正在播放 if (isPlaying == true) { Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "pause"); startService(intent); isPlaying = false; imageBtnPlay.setImageResource(R.drawable.play1); replaying=false; } else { Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "playing"); intent.putExtra("id", id); startService(intent); isPlaying = true; imageBtnPlay.setImageResource(R.drawable.pause1); replaying=true; } } else if (v == imageBtnForward) { // 下一首 int id=MusicService._id+1; if(id>=lists.size()-1){ id=lists.size()-1; }else if(id<=0){ id=0; } Music m = lists.get(id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "forward"); intent.putExtra("id", id); startService(intent); isPlaying = true; } else if (v == imageBtnNext) { // 最后一首 int id=lists.size()-1; Music m = lists.get(id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "last"); intent.putExtra("id", id); startService(intent); isPlaying = true; } else if (v == imageBtnLoop) { if (isLoop == true) { // 顺序播放 imageBtnLoop .setBackgroundResource(R.drawable.play_loop_spec); isLoop = false; } else { // 单曲播放 imageBtnLoop .setBackgroundResource(R.drawable.play_loop_sel); isLoop = true; } } else if (v == imageBtnRandom) { imageBtnRandom.setImageResource(R.drawable.play_random_sel); }}
} private class MyCompletionListner extends BroadcastReceiver{@Override
public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Music m = lists.get(MusicService._id); textName.setText(m.getTitle()); textSinger.setText(m.getSinger()); textEndTime.setText(toTime((int) m.getTime())); imageBtnPlay.setImageResource(R.drawable.pause1); } } /** * 时间格式转换 * * @param time * */ public String toTime(int time) {time /= 1000;
int minute = time / 60; int hour = minute / 60; int second = time % 60; minute %= 60; return String.format("%02d:%02d", minute, second); } //重力感应 甩歌代码 private static final int SHAKE_THRESHOLD = 3000; private long lastUpdate=0; private double last_x=0; private double last_y= 4.50; private double last_z=9.50; //这个控制精度,越小表示反应越灵敏 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub //处理精准度改变 }@Override
public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ long curTime = System.currentTimeMillis(); // 每200毫秒检测一次 if ((curTime - lastUpdate) > 100) { long diffTime = (curTime - lastUpdate); lastUpdate = curTime; double x=event.values[SensorManager.DATA_X]; double y=event.values[SensorManager.DATA_Y]; double z=event.values[SensorManager.DATA_Z]; Log.e("---------------", "x="+x+" y="+y+" z="+z); float speed = (float) (Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000); if (speed > SHAKE_THRESHOLD) { //检测到摇晃后执行的代码 if(MusicService.playing==true){ Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "pause"); startService(intent); isPlaying = false; imageBtnPlay.setImageResource(R.drawable.play1); replaying=false; }else{ Intent intent = new Intent(MusicActivity.this, MusicService.class); intent.putExtra("play", "playing"); intent.putExtra("id", id); startService(intent); isPlaying = true; imageBtnPlay.setImageResource(R.drawable.pause1); replaying=true; } } last_x = x; last_y = y; last_z = z; } } }}
5,main
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); Resources res = getResources(); TabHost tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent; intent = new Intent().setClass(this, ListActivity.class); spec = tabHost.newTabSpec("音乐").setIndicator("音乐", res.getDrawable(R.drawable.item)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, ArtistsActivity.class); spec = tabHost.newTabSpec("艺术家").setIndicator("艺术家", res.getDrawable(R.drawable.artist)) .setContent(intent); tabHost.addTab(spec);intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("专辑").setIndicator("专辑", res.getDrawable(R.drawable.album)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放", res.getDrawable(R.drawable.album)) .setContent(intent); tabHost.addTab(spec);tabHost.setCurrentTab(0);
}
}