Android之简易音乐播发器
布局主要代码之ListView:
<span style="font-size:14px;"> <ListView android:id="@+id/music_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/handle_btnlayout" android:cacheColorHint="#00000000" /*设置为透明,防止滑动时背景变成白色*/ android:dividerHeight="1dip" android:divider="#88888888" /*设置分割线的颜色和透明度*/ android:listSelector="#00000000" android:layout_marginBottom="50dp" > </ListView></span>
Android之UI界面代码:
<span style="font-size:14px;">public class Music extends BaseActivity{ //BaseActivity继承Activity作为所有活动的父类 public static final String ACTION_UPDATEUI = "action.updateUI"; private ListView ls = null; private Button play_music = null; private Button previous_music =null; private Button next_music =null; private Button repeat_music = null; private Button shuffle_music =null; private TextView music_title = null; private TextView music_duration =null; public static List<Map<String, String>> list ; private Boolean start = false; private Boolean isrepeat = MusicService.repeat; UpdateUIBroadcastReceiver broadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); super.setContentView(R.layout.home_activity_layout); play_music = (Button) findViewById(R.id.play_music); previous_music = (Button) findViewById(R.id.previous_music); next_music = (Button) findViewById(R.id.next_music); repeat_music = (Button) findViewById(R.id.repeat_music); shuffle_music = (Button) findViewById(R.id.shuffle_music); music_title = (TextView) findViewById(R.id.music_title); music_duration = (TextView) findViewById(R.id.music_duration); music_title.setText(MusicService.music_title); music_duration.setText(MusicService.music_time); play_music.setOnClickListener(new ButtonsetOnClickListener()); previous_music.setOnClickListener(new ButtonsetOnClickListener()); next_music.setOnClickListener(new ButtonsetOnClickListener()); repeat_music.setOnClickListener(new ButtonsetOnClickListener()); shuffle_music.setOnClickListener(new ButtonsetOnClickListener()); if(MusicService.isplay){ play_music.setBackgroundResource(R.drawable.pause_selector); }else{ play_music.setBackgroundResource(R.drawable.play_selector); } if(isrepeat){ repeat_music.setBackgroundResource(R.drawable.repeat_all_selector); }else{ shuffle_music.setBackgroundResource(R.drawable.shuffle_selector); } //动态注册广播接收器 IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_UPDATEUI); broadcastReceiver = new UpdateUIBroadcastReceiver(); registerReceiver(broadcastReceiver, filter); ls = (ListView) findViewById(R.id.music_list); //接下来要将这些数据封装成ArrayAdapter对象 SimpleAdapter adapter = this.getAdapter(); ls.setAdapter(adapter); ls.setOnItemClickListener(new ItemClick()); //点击 ls.setOnItemLongClickListener(null); //长按 ls.setOnItemSelectedListener(null); //勾选 } private class ButtonsetOnClickListener implements OnClickListener{ Intent intent =new Intent(Music.this, MusicService.class); //记得注册Service Bundle bundle = new Bundle(); @Override public void onClick(View v) { switch(v.getId()){ case R.id.play_music: if(start){ play_music.setBackgroundResource(R.drawable.play_selector); bundle.putBoolean("start", false); start = false; }else{ play_music.setBackgroundResource(R.drawable.pause_selector); bundle.putBoolean("start", true); start = true; } break; case R.id.previous_music: bundle.putBoolean("previous", true); break; case R.id.next_music: bundle.putBoolean("next", true); break; case R.id.repeat_music: bundle.putBoolean("repeat", true); repeat_music.setBackgroundResource(R.drawable.repeat_all_selector); shuffle_music.setBackgroundResource(R.drawable.shuffle_none_selector); break; case R.id.shuffle_music: bundle.putBoolean("shuffle", true); shuffle_music.setBackgroundResource(R.drawable.shuffle_selector); repeat_music.setBackgroundResource(R.drawable.repeat_none_selector); break; default: break; } intent.putExtras(bundle); //Intent传递参数 Music.this.startService(intent); //启动服务 } } //定义广播接收器(内部类) private class UpdateUIBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { music_title.setText(String.valueOf(intent.getExtras().getString("music_title"))); music_duration.setText(String.valueOf(intent.getExtras().getString("music_time"))); } } @SuppressLint("SimpleDateFormat") private SimpleAdapter getAdapter(){ // 使用SimpleAdapter来封装数据时必需要把每个显示的数据封装成Map list=new ArrayList<Map<String, String>>(); SimpleDateFormat format = new SimpleDateFormat("mm:ss"); //取得所有在外部存储卡上的音乐文件的信息 ps : MediaStore.Audio.Media.INTERNAL_CONTENT_URI 获得内置存储 Cursor cursor = Music.this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); cursor.moveToFirst(); for(int i=0;i<cursor.getCount();i++){ String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); //歌曲的名称 String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); //歌曲文件的路径 int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); //歌曲的总播放时长 String hmsString = format.format(duration); Map<String, String>map =new HashMap<String, String>(); map.put("title", title); map.put("time", hmsString); map.put("url", url); list.add(map); cursor.moveToNext(); } SimpleAdapter adapter =new SimpleAdapter(this, //上下文,显示对象 list, // 要显示的数据,一定要上面的格式进行封装 R.layout.music_list, // 列表中要显示的单项信息的布局 new String[]{"title","time"}, // map中的key new int[]{R.id.music_title_my,R.id.music_time_my}); // 显示到组件 return adapter; } //点击事件 private class ItemClick implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent =new Intent(Music.this, MusicService.class); //记得注册Activity Bundle bundle = new Bundle(); bundle.putBoolean("playing", true); bundle.putInt("position", position); intent.putExtras(bundle); //Intent传递参数 Music.this.startService(intent); //启动服务 play_music.setBackgroundResource(R.drawable.pause_selector); } } } </span>
<span style="font-size:14px;">public class MusicService extends Service{ public static String music_title =""; public static String music_time =""; private MediaPlayer mp ; private List<Map<String, String>> list = Music.list; private int position ; private boolean playing = false; private boolean start = true; private boolean previous = false; private boolean next =false; public static boolean repeat = true; private boolean shuffle = false; public static boolean isplay = false; Random random = new Random(); @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate(){ super.onCreate(); /* final Intent intent = new Intent(); intent.setAction(Music.ACTION_UPDATEUI); timer = new Timer(); task = new TimerTask() { @Override public void run() { intent.putExtra("count", ++count); sendBroadcast(intent); } }; timer.schedule(task, 1000,1000); */ } @SuppressLint("NewApi") @Override public int onStartCommand(Intent intent, int flags, int startId){ start = intent.getBooleanExtra("start", true); //取不到值,默认false playing = intent.getBooleanExtra("playing", false); previous = intent.getBooleanExtra("previous", false); next = intent.getBooleanExtra("next", false); if(repeat){ shuffle = intent.getBooleanExtra("shuffle", false); if(shuffle){ repeat = false; } }else if(shuffle){ repeat = intent.getBooleanExtra("repeat", false); if(repeat){ shuffle = false; } } if(playing){ position = intent.getIntExtra("position",1); Uri uri = Uri.parse(list.get(position).get("url")); playsong(uri); }else if(previous){ previoussong(); }else if(next){ nextsong(); } if(mp!=null){ if(start){ mp.start(); isplay = true; }else{ mp.pause(); //暂停播放 isplay = false; } } return super.onStartCommand(intent, flags, startId); } private void playsong(Uri uri){ if(mp!=null&&mp.isPlaying()){ mp.stop(); //停止播放 } mp = MediaPlayer.create(this, uri); mp.start(); mp.setOnCompletionListener(new CompletionListener()); music_title = list.get(position).get("title"); music_time = list.get(position).get("time"); //发送广播,改变界面 Intent intent = new Intent(); intent.setAction(Music.ACTION_UPDATEUI); Bundle bundle = new Bundle(); bundle.putString("music_title",music_title) ; bundle.putString("music_time", music_time); intent.putExtras(bundle); sendBroadcast(intent); } //获取随机数 private int randomInt(){ int num = random.nextInt(list.size()); if(num>=1&&num<=list.size()-1){ return num; }else{ return randomInt(); } } private void previoussong(){ if(repeat){ position--; if(position<=1){ position = list.size()-1; } }else{ position = randomInt(); } String urlstr = list.get(position).get("url"); Uri uri = Uri.parse(urlstr); playsong(uri); } private void nextsong(){ if(repeat){ position++; if(position>=list.size()){ position = 1; } }else{ position = randomInt(); } String urlstr = list.get(position).get("url"); Uri uri = Uri.parse(urlstr); playsong(uri); } private class CompletionListener implements OnCompletionListener{ @Override public void onCompletion(MediaPlayer mp) { nextsong(); } } @Override public void onDestroy(){ super.onDestroy(); if(mp!=null) mp.release(); //释放资源 stopSelf(); // timer.cancel(); } }</span>