1. 涉及到的技术点
- Service服务的使用
- MediaPlayer的使用
2. 开发环境
- 开发工具:AndroidStudio
- 开发语言:java
- jdk版本:8.0+以上
3. 需求分析
在项目中,去单独实现一个音乐播放模块功能,具体需要实现音乐暂停和播放
4. 实现步骤
- 编写一个AudioServiceOnBind.java类去继承Service,并且实现MediaPlayer.OnCompletionListener
public class AudioServiceOnBind extends Service implements MediaPlayer.OnCompletionListener{
}
- 在onCreate()方法里面初始化MediaPlayer
@Override
public void onCreate(){
super.onCreate();
Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onCreate()");
if (mediaPlayer==null){
mediaPlayer=MediaPlayer.create(this,R.raw.music);
mediaPlayer.setOnCompletionListener(this);
}
}
- 在onDestroy()方法里面需要暂停,并释放资源
@Override
public void onDestroy(){
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.release();
stopForeground(true);
Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onDestroy()");
}
- 暂停方法实现
/**
* 暂停
*/
public void pause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
- 播放方法实现
/***
* 播放
*/
public void play() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
- 整个类的代码实现过程
public class AudioServiceOnBind extends Service implements MediaPlayer.OnCompletionListener {
private final IBinder binder = new AudioBinder();
//用于播放音乐等媒体资源
private MediaPlayer mediaPlayer;
public AudioServiceOnBind() {
super();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate(){
super.onCreate();
Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onCreate()");
if (mediaPlayer==null){
mediaPlayer=MediaPlayer.create(this,R.raw.music);
mediaPlayer.setOnCompletionListener(this);
}
}
@Override
public void onCompletion(MediaPlayer mp) {
stopSelf();
}
@Override
public void onDestroy(){
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.release();
stopForeground(true);
Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onDestroy()");
}
/**
* 暂停
*/
public void pause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
/***
* 播放
*/
public void play() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
//为了和Activity交互,我们需要定义一个Binder对象
class AudioBinder extends Binder {
//返回Service对象
AudioServiceOnBind getService(){
return AudioServiceOnBind.this;
}
}
}
温馨提示,将music.mp3文件放在res->raw中,默认没有raw文件夹,需要新建,如下图所示
7.编写activity_music.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MusicActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_200"
app:navigationIcon="@mipmap/img_back"
app:title="给我一个理由忘记 - 黄丽玲"
app:titleTextColor="@color/white" />
<androidx.cardview.widget.CardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
app:cardCornerRadius="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/music_bg" />
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:gravity="center">
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="暂停" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
布局效果图如下
- MusicActivity页面具体代码实现过程
public class MusicActivity extends AppCompatActivity {
private AudioServiceOnBind audioServiceOnBind;
//使用ServiceConnection来监听Service状态的变化
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
audioServiceOnBind = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
//这里我们实例化audioService,通过binder来实现
audioServiceOnBind = ((AudioServiceOnBind.AudioBinder) binder).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
//绑定服务
Intent intentService = new Intent(MusicActivity.this, AudioServiceOnBind.class);
bindService(intentService, conn, Context.BIND_AUTO_CREATE);
//播放
findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
audioServiceOnBind.play();
}
});
findViewById(R.id.pause).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
audioServiceOnBind.pause();
}
});
}
}