需求:播放音乐,暂停音乐,停止音乐,退出
Activity版
private Button btn_play;
private Button btn_pause;
private Button btn_stop;
private Button btn_exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_play = (Button) findViewById(R.id.btn_play);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_exit = (Button) findViewById(R.id.btn_exit);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_exit.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_play.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (btn_play == v) {
play();
} else if (btn_stop == v) {
stopMusic();
} else if (btn_exit == v) {
exit();
} else if (btn_pause == v) {
pauseMesic();
}
}
private MediaPlayer player;
private void pauseMesic() {
if (player!=null && player.isPlaying()) {
player.pause();
}
}
private void exit() {
stopMusic();
finish();
}
private void stopMusic() {
if (player!=null) {
player.stop();
player.reset();
player.release();
player = null;
}
}
private void play() {
if (player == null) {
player = MediaPlayer.create(this,R.raw.water_hander);
}
player.start();
}
这一种有一个大Bug,在点击返回键后音乐继续播放,但是重新打开软件时,点击其余的按钮都没有反应,这就是Activity与Service的区别,接着Service端
private Button btn_play;
private Button btn_pause;
private Button btn_stop;
private Button btn_exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_play = (Button) findViewById(R.id.btn_play);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_exit = (Button) findViewById(R.id.btn_exit);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_exit.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_play.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this,PlayMusic.class);
if (btn_play == v) {
intent.putExtra("action","play");
startService(intent);
} else if (btn_stop == v) {
intent.putExtra("action","stop");
startService(intent);
} else if (btn_exit == v) {
stopService(intent);
finish();
} else if (btn_pause == v) {
intent.putExtra("action","pause");
startService(intent);
}
}
需要新建一个Service用于上面几个按钮的实现
package shangguigu.java.playmusic;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class PlayMusic extends Service {
public PlayMusic() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
private MediaPlayer player;
private void pauseMesic() {
if (player!=null && player.isPlaying()) {
player.pause();
}
}
private void stopMusic() {
if (player!=null) {
player.stop();
player.reset();
player.release();
player = null;
}
}
private void play() {
if (player == null) {
player = MediaPlayer.create(this,R.raw.water_hander);
}
player.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getStringExtra("action");
if ("play".equals(action)) {
play();
} else if ("pause".equals(action)) {
pauseMesic();
} else if ("stop".equals(action)) {
stopMusic();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
stopMusic();
}
}
调用service时需要使用onStartCommand方法