大家好,音乐播放器的1.1版本已经完成了,基本的功能还是有的,但是有个问题因为是在Activity中实现音乐播放,所以退出之后音乐就不能播放啦,而我们需要它在后台也能播放怎么办呢,那就要用到Service这个类啦!
上一个版本的xml文件不要改动,只要改动Java文件和新增一个类,不多说上代码:
MusicPlayer.java:
private Button button_start; //音乐开始/暂停按钮
private Button button_stop; //音乐停止按钮
private Button button_loop; //音乐重复/取消重复按钮
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
setListener();
}
private void findView() {
//为每个button都setId是为了按钮监听时分辨出具体的button
button_start = (Button)findViewById(R.id.button_start);
button_start.setId(0);
button_stop = (Button)findViewById(R.id.button_stop);
button_stop.setId(1);
button_loop = (Button)findViewById(R.id.button_loop);
button_loop.setId(2);
}
private void setListener(){
//给每个按钮绑定事件
button_start.setOnClickListener(buttonListener);
button_stop.setOnClickListener(buttonListener);
button_loop.setOnClickListener(buttonListener);
}
private Button.OnClickListener buttonListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
//根据之前为每个Button设置的Id值实现对应的功能
switch(v.getId()) {
case 0 :
//启动Service;
Intent intent_start = new Intent(MusicService.PLAY_ACTION);
intent_start.setClass(MusicPlayer.this, MusicService.class);
startService(intent_start);
//按钮显示文字与播放与暂停功能的互换
if(button_start.getText()=="暂停") {
button_start.setText("播放");
Intent intent_pause = new Intent(MusicService.PAUSE_ACTION);
intent_pause.setClass(MusicPlayer.this, MusicService.class);
startService(intent_pause);
}else {
button_start.setText("暂停");
}
break;
case 1 :
if(button_start.getText()=="暂停") {
button_start.setText("播放");
//player.pause();
Intent intent_pause = new Intent(MusicService.PAUSE_ACTION);
intent_pause.setClass(MusicPlayer.this, MusicService.class);
startService(intent_pause);
}
Intent intent_stop = new Intent(MusicService.STOP_ACTION);
intent_stop.setClass(MusicPlayer.this, MusicService.class);
startService(intent_stop);
break;
case 2 :
Intent intent_loop= new Intent(MusicService.LOOP_ACTION);
intent_loop.setClass(MusicPlayer.this, MusicService.class);
startService(intent_loop);
//按钮显示文字与功能的互换
if(button_loop.getText()=="取消重复") {
button_loop.setText("重复");
Intent intent_notLoop= new Intent(MusicService.NOTLOOP_ACTION);
intent_notLoop.setClass(MusicPlayer.this, MusicService.class);
startService(intent_notLoop);
Toast.makeText(MusicPlayer.this, "重复已关闭", Toast.LENGTH_SHORT).show();
}else {
button_loop.setText("取消重复");
Toast.makeText(MusicPlayer.this, "重复已开启", Toast.LENGTH_SHORT).show();
}
break;
}
}
};
//按下Menu键时会执行这个方法
public boolean onCreateOptionsMenu(Menu menu) {
//增加两个选项
menu.add(0, 0, 0, R.string.menu_about);
menu.add(0, 1, 0, R.string.menu_exit);
return super.onCreateOptionsMenu(menu);
}
//选择Menu中的选项是会执行这个方法
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case 0:
//创建新的Dialog
new AlertDialog.Builder(MusicPlayer.this).setTitle(R.string.menu_about).
setMessage(R.string.about_message).setPositiveButton(R.string.dialog_yes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
break;
case 1:
finish();
break;
}
return true;
}
}
把MusicPlayer.Java中播放音乐的代码去掉啦,在MusicService.java这个类中实现让音乐能在后台播放。
MusicService.java:
public static final String TAG = "MusicService";
public static final String PLAY_ACTION = "com.android.musicPlayer.PLAY_ACTION";
public static final String PAUSE_ACTION = "com.android.musicPlayer.PAUSE_ACTION";
public static final String STOP_ACTION = "com.android.musicPlayer.STOP_ACTION";
public static final String LOOP_ACTION = "com.android.musicPlayer.LOOP_ACTION";
public static final String NOTLOOP_ACTION = "com.android.musicPlayer.NOTLOOP_ACTION";
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
super.onCreate();
init();
//多用Log来输出信息来了解Service的生命周期
Log.d(TAG, "onCreate__");
}
public void onStart(Intent intent,int startId) {
super.onStart(intent, startId);
//将得到的Intent中带的值赋值给字符串action用来判断应该实现什么功能
String action = intent.getAction();
if(action.equals(PLAY_ACTION)) {
play();//播放
}else if(action.equals(PAUSE_ACTION)) {
pause();//暂停
}else if(action.equals(STOP_ACTION)) {
stop();//停止
}else if(action.equals(LOOP_ACTION)) {
looping(true);//重复
}else if(action.equals(NOTLOOP_ACTION)) {
looping(false);//取消重复
}
Log.d(TAG, "onStart__");
}
private void init() {
player = MediaPlayer.create(MusicService.this, R.raw.example);
/*
* MediaPlayer可以由create()这个静态方法创建可以有三种不同的参数,我用的是(Context context, Uri uri)
* 第一个参数是我们的这个Activity第二个是放入到raw文件夹中的音乐资源
*/
}
private void play() {
player.start();
Log.d(TAG, "start------");
}
private void pause() {
player.pause();
Log.d(TAG, "pause------");
}
private void stop() {
player.stop();
try {
//必须要先调用prepare()这个方法不然停止之后就不能再重新播放
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "stop------");
}
private void looping(boolean flag) {
player.setLooping(flag);
}
public void onDestory() {
super.onDestroy();
player.release();
}
}
最后在AndroidManifest.xml 中加入:
<service android:name=".MusicService" android:exported="true"></service>
不然会报错的!
得到的程序比1.1版本多实现了在后台播放的功能,其余都一样,现在我也更深刻的了解了Activity的生命周期和Service的生命周期,有时间写写关于它们博客总结一下,呵呵,1.2版本完成啦,本人能力现在还有限,不足之处请大家多包涵,很欢迎大家和我交流。下个版本再见。