java - 如何让按钮暂停记住 MediaPlayer 停止的位置?
问题描述
我前段时间制作了一个 MediaPlayer,但遇到了一个大问题。音频无法在后台播放,所以我必须使用服务。此时,我设法播放音乐并将其停止,但是当我再次单击播放时,它会重新启动,我不知道该怎么办。我是编码新手。除此之外,我想为玩家位置制作一个工作搜索栏和一个 textView,但那是另一次了。如果有人帮助我,我将不胜感激。
这是我现在使用的代码:
btPause = findViewById(R.id.btPauseMusic);
btPlay = findViewById(R.id.btPlayMusic);
btPlay.setOnClickListener(v -> {
startService(new Intent(this, BackgroundMusicService.class));
btPlay.setVisibility(View.GONE);
btPause.setVisibility(View.VISIBLE);
});
btPause.setOnClickListener(v -> {
stopService(new Intent(this, BackgroundMusicService.class));
btPause.setVisibility(View.GONE);
btPlay.setVisibility(View.VISIBLE);
});
}
服务:
public class BackgroundMusicService extends Service {
private MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = MediaPlayer.create(this, R.raw.ding_dong);
mediaPlayer.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
}
}
解决方案
BackgroundMusicService
在你里面添加这个通知代码onCreate
。
Intent stopSelf = new Intent(this, BackgroundMusicService.class);
stopSelf.setAction("Stop");
PendingIntent pStopSelf = PendingIntent
.getService(this, 0, stopSelf
, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification;
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(
0, "Pause", pStopSelf
).build();
Intent startSelf = new Intent(this, BackgroundMusicService.class);
stopSelf.setAction("Start");
PendingIntent pstartSelf = PendingIntent
.getService(this, 0, stopSelf
, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notifications;
NotificationCompat.Action actions =
new NotificationCompat.Action.Builder(
0, "Play", pstartSelf
).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel("channal", "Notification", NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(serviceChannel);
notification = new NotificationCompat.Builder(this, "channal")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Music.")
.setPriority(Notification.PRIORITY_MAX)
.addAction(action)
.addAction(actions).build();
} else {
notification = new NotificationCompat.Builder(this, "channal")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("App name")
.setContentText("Music.")
.setPriority(Notification.PRIORITY_MAX)
.addAction(action)
.addAction(actions)
.build();
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
startForeground(1, notification);
在onStartCommand
您的意图操作中,您可以使用length
.
int length=0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if ("STOP".equals(intent.getAction())) {
//inside here pause the music.
mediaPlayer.pause();
length = mediaPlayer.getCurrentPosition();
}else if ("Start".equals(intent.getAction())) {
//inside here play the music with seekto by length where the music was.
mediaPlayer.start();
mediaPlayer.seekTo(length);
}
return START_STICKY;
}
注意:如果您仍然不明白任何内容,可以在评论中问我。
推荐阅读
- html - 如何在 knitr R 代码块中间插入 HTML?
- nginx - NGINX HTTP/HTTPS 重定向 Elasticsearch 中断?
- c# - ASP.NET Core MVC - 将模型数据从视图传递回控制器
- angular - 添加星号的最佳方法
有棱角的材料 - sas - 将多个变量转置到以日期为后缀的列
- python - 无法按类名查找
- python-3.x - Vpython访问从pdb蛋白质文件加载的对象
- c# - 如何根据c#中方法的参数设置数组的大小?
- proxy - 我无法通过代理访问我的 apollo 服务器
- java - 带有 Redis Sentinel 的 Spring Boot 缓存始终连接到主节点