使用Service实现音乐播放器
1.首先创一个Service类 代码如下
package com.example.myapplication5;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
public class MyService extends Service {
MediaPlayer mediaPlayer;
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new Binder();
}
class Binder extends android.os.Binder {
public void start() {
startmusic();
}
public void stop() {
stopmusic();
}
}
private void startmusic() {
Log.e("---", "aaa");
mediaPlayer.reset(); // 重置播放状态
try {
mediaPlayer.setDataSource("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4");
mediaPlayer.prepareAsync(); // 异步准备
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { // 异步监听
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// 下载完成 播放
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopmusic() {
mediaPlayer.stop();
}
}
2.然后在清单文件加入服务
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
3.然后在Mactivity.java里面写实现代码
package com.example.myapplication5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
MyService.Binder binder;
private Button startmusic;
private Button stopmusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startmusic = (Button) findViewById(R.id.startmusic);
stopmusic = (Button) findViewById(R.id.stopmusic);
initService();
/**
*
* 开始播放
*/
startmusic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binder.start();
}
});
/**
* 暂停
*/
stopmusic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binder.stop();
}
});
}
public void initService() {
requestPermissions(new String[]{
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.READ_EXTERNAL_STORAGE"
}, 99);
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
binder = (MyService.Binder) iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
bindService(new Intent(MainActivity.this, MyService.class), serviceConnection, BIND_AUTO_CREATE);
}
}
4.main里面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/startmusic"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:id="@+id/stopmusic"
/>
</LinearLayout>