如何让按钮暂停记住 MediaPlayer 停止的位置?

How can I make the button pause remember where the MediaPlayer stopped?

前段时间做了一个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;
    }

注意:如果还有什么不明白的可以在评论里问我