Android基于Service的音乐播放器
本文开发一个基于Service的音乐播放器,音乐由后台运行的Service负责播放,当后台的播放状态发生变化时,程序将会通过发送广播通知前台Activity更新界面;当点击Activity的界面按钮时,系统将通过发送广播通知后台Service来改变播放状态。
前台Activity界面有两个按钮,分别用于控制播放/暂停、停止,另外还有两个文本框,用于显示正在播放的歌曲名、歌手名。前台Activity的代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ImageButton mStart; private ImageButton mStop; private TextView mMusicName; private TextView mSongerName; private ActivityReceiver mActivityReceiver; public static final String CTL_ACTION = "com.trampcr.action.CTL_ACTION"; public static final String UPDATE_ACTION = "com.trampcr.action.UPDATE_ACTION"; //定义音乐播放状态,0x11代表没有播放,0x12代表正在播放,0x13代表暂停 int status = 0x11; String[] musicNames = new String[]{"完美生活", "那一年", "故乡"}; String[] songerNames = new String[]{"许巍", "许巍", "许巍"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mStart = (ImageButton) findViewById(R.id.start); mStop = (ImageButton) findViewById(R.id.stop); mMusicName = (TextView) findViewById(R.id.music_name); mSongerName = (TextView) findViewById(R.id.songer_name); mStart.setOnClickListener(this); mStop.setOnClickListener(this); mActivityReceiver = new ActivityReceiver(); //创建IntentFilter IntentFilter filter = new IntentFilter(); //指定BroadcastReceiver监听的Action filter.addAction(UPDATE_ACTION); //注册BroadcastReceiver registerReceiver(mActivityReceiver, filter); Intent intent = new Intent(MainActivity.this, MusicService.class); //启动后台Service startService(intent); } public class ActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //获取Intent中的update消息,update代表播放状态 int update = intent.getIntExtra("update", -1); //获取Intent中的current消息,current代表当前正在播放的歌曲 int current = intent.getIntExtra("current", -1); if (current >= 0){ mMusicName.setText(musicNames[current]); mSongerName.setText(songerNames[current]); } switch (update){ case 0x11: mStart.setBackgroundResource(R.drawable.play); status = 0x11; break; //控制系统进入播放状态 case 0x12: //在播放状态下设置使用暂停图标 mStart.setBackgroundResource(R.drawable.pause); status = 0x12; break; case 0x13: //在暂停状态下设置使用播放图标 mStart.setBackgroundResource(R.drawable.play); status = 0x13; break; } } } @Override public void onClick(View v) { Intent intent = new Intent(CTL_ACTION); switch (v.getId()){ case R.id.start: intent.putExtra("control", 1); break; case R.id.stop: intent.putExtra("control", 2); break; } //发送广播,将被Service中的BroadcastReceiver接收到 sendBroadcast(intent); } }
ActivityReceiver()用于响应后台Service所发出的广播,该程序将会根据广播Intent里的消息来改变播放状态,并更新程序界面中按钮的图标。
onClick中根据点击的按钮发送广播,发送广播时会把所按下的按钮标识发送出来。
接下来是后台Service,会在播放状态发生改变时对外发送广播。代码如下:
public class MusicService extends Service { MyReceiver serviceReceiver; AssetManager mAssetManager; String[] musics = new String[]{"prefectLife.mp3", "thatYear.mp3", "country.mp3"}; MediaPlayer mMediaPlayer; int status = 0x11; int current = 0; // 记录当前正在播放的音乐 @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mAssetManager = getAssets(); serviceReceiver = new MyReceiver(); //创建IntentFilter IntentFilter filter = new IntentFilter(); filter.addAction(MainActivity.CTL_ACTION); registerReceiver(serviceReceiver, filter); //创建MediaPlayer mMediaPlayer = new MediaPlayer(); //为MediaPlayer播放完成事件绑定监听器 mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { current++; if (current >= 3) { current = 0; } //发送广播通知Activity更改文本框 Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION); sendIntent.putExtra("current", current); //发送广播,将被Activity中的BroadcastReceiver接收到 sendBroadcast(sendIntent); //准备并播放音乐 prepareAndPlay(musics[current]); } }); } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int control = intent.getIntExtra("control", -1); switch (control){ case 1: // 播放或暂停 //原来处于没有播放状态 if (status ==0x11){ //准备播放音乐 prepareAndPlay(musics[current]); status = 0x12; } //原来处于播放状态 else if (status == 0x12){ //暂停 mMediaPlayer.pause(); status = 0x13; // 改变为暂停状态 } //原来处于暂停状态 else if (status == 0x13){ //播放 mMediaPlayer.start(); status = 0x12; // 改变状态 } break; //停止声音 case 2: //如果原来正在播放或暂停 if (status == 0x12 || status == 0x13){ //停止播放 mMediaPlayer.stop(); status = 0x11; } } //广播通知Activity更改图标、文本框 Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION); sendIntent.putExtra("update", status); sendIntent.putExtra("current", current); //发送广播,将被Activity中的BroadcastReceiver接收到 sendBroadcast(sendIntent); } } private void prepareAndPlay(String music) { try { //打开指定的音乐文件 AssetFileDescriptor assetFileDescriptor = mAssetManager.openFd(music); mMediaPlayer.reset(); //使用MediaPlayer加载指定的声音文件 mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength()); mMediaPlayer.prepare(); // 准备声音 mMediaPlayer.start(); // 播放 } catch (IOException e) { e.printStackTrace(); } } }
MyReceiver用于接收前台Activity所发出的广播,并根据广播的消息内容改变Service的播放状态,当播放状态改变时,该Service对外发送一条广播,广播消息将会被前台Activity接收,前台Activity将会根据广播消息更新界面。
为了让该音乐播放器能按顺序依次播放歌曲,程序为MediaPlayer增加了OnCompletionListener监听器,当MediaPlayer播放完成后将自动播放下一首歌曲。
运行程序,效果图如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)