播放音乐的行为写在服务里,Activity去调用Service里面到方法,进行音乐播放,当Activity结束后,音乐播放器停止播放
界面:
MainActivity:
package liudeli.service1; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Environment; import android.os.IBinder; import android.view.View; import android.widget.Button; import android.widget.Toast; import liudeli.service1.service.MyService5; import liudeli.service1.service.inter.IMusicPlay; public class MainActivity5 extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main5); initView(); initListener(); } @Override protected void onStart() { super.onStart(); // 绑定服务 bindService(new Intent(this, MyService5.class), connection, BIND_AUTO_CREATE); } private Button btPlayMusic; private Button btPausedContinue; private Button btStop; private void initView() { btPlayMusic = findViewById(R.id.bt_play_music); btPausedContinue = findViewById(R.id.bt_paused_continue); btStop = findViewById(R.id.bt_stop); } private void initListener() { btPlayMusic.setOnClickListener(this); btPausedContinue.setOnClickListener(this); btStop.setOnClickListener(this); } private IMusicPlay iMusicPlay; private ServiceConnection connection = new ServiceConnection() { /** * 连接到服务 * @param name * @param service */ @Override public void onServiceConnected(ComponentName name, IBinder service) { iMusicPlay = (IMusicPlay) service; } /** * 断开连接 * @param name */ @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onDestroy() { super.onDestroy(); // 解绑服务:注意bindService后 必须要解绑服务,否则会报 连接资源异常 if (null != connection) { unbindService(connection); } } // 音乐文件到路径 private final String MUSIC_PATH = Environment.getExternalStorageDirectory() + "/cjyy.mp3"; @Override public void onClick(View v) { if (iMusicPlay == null) { Toast.makeText(this, "音乐播放服务连接失败", Toast.LENGTH_LONG).show(); return; } switch (v.getId()) { case R.id.bt_play_music: iMusicPlay.playMusic(MUSIC_PATH); break; case R.id.bt_paused_continue: if ("暂停播放".equals(btPausedContinue.getText().toString())) { btPausedContinue.setText("继续播放"); iMusicPlay.pausedPlay(); } else { btPausedContinue.setText("暂停播放"); iMusicPlay.continuePlay(); } break; case R.id.bt_stop: iMusicPlay.stopPlay(); break; default: break; } } }
增强版Binder扩展,接口定义:
package liudeli.service1.service.inter; public interface IMusicPlay { /** * 播放音乐 * @param musicPath 音乐文件的路径 */ void playMusic(String musicPath); /** * 暂停播放 */ void pausedPlay(); /** * 继续播放 */ void continuePlay(); /** * 停止播放 */ void stopPlay(); }
Service代码:
package liudeli.service1.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.util.Log; import java.io.IOException; import liudeli.service1.service.inter.IMusicPlay; public class MyService5 extends Service { private final String TAG = MyService5.class.getSimpleName(); @Override public IBinder onBind(Intent intent) { Log.d(TAG, "绑定成功"); return new PlayMusicBinder(); } private MediaPlayer mediaPlayer; /** * 增强版Binder,扩展出播放音乐🎵行为 */ class PlayMusicBinder extends Binder implements IMusicPlay { public PlayMusicBinder() { mediaPlayer = new MediaPlayer(); } /** * 播放音乐 * * @param musicPath 音乐文件的路径 */ @Override public void playMusic(String musicPath) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(musicPath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } /** * 暂停播放 */ @Override public void pausedPlay() { mediaPlayer.pause(); } /** * 继续播放 */ @Override public void continuePlay() { mediaPlayer.start(); } /** * 停止播放 */ @Override public void stopPlay() { mediaPlayer.stop(); } } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "解绑成功"); // 为什么解绑服务了,音乐还在播放,应该MediaPlay内部是一个服务 if (mediaPlayer != null) { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } mediaPlayer.release(); // 释放硬件播放资源 } return super.onUnbind(intent); } }