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

Java 如何使按钮暂停?记住MediaPlayer停止的位置?,java,android-mediaplayer,seekbar,Java,Android Mediaplayer,Seekbar,不久前我做了一个MediaPlayer,我面临一个大问题。音频不能在后台播放,所以我必须使用服务。在这一点上,我设法播放音乐,并停止它,但当我再次点击播放,它重新启动,我不知道该怎么办。我不熟悉编码。除此之外,我想为玩家的位置制作一个工作搜索栏和一个文本视图,但这是另一次。如果有人帮助我,我会非常感激 以下是我现在使用的代码: btPause = findViewById(R.id.btPauseMusic); btPlay = findViewById(R.id.btPlayMusic);

不久前我做了一个MediaPlayer,我面临一个大问题。音频不能在后台播放,所以我必须使用服务。在这一点上,我设法播放音乐,并停止它,但当我再次点击播放,它重新启动,我不知道该怎么办。我不熟悉编码。除此之外,我想为玩家的位置制作一个工作搜索栏和一个文本视图,但这是另一次。如果有人帮助我,我会非常感激

以下是我现在使用的代码:

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();
}
}

onCreate
上的
BackgroundMusicService
内部添加此通知代码

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;
    }

注意:如果您仍然不理解任何内容,可以在评论中询问我。

添加您正在停止并再次播放的代码。这是我添加的第一个代码。BTL在我设置的位置播放和暂停ClickListener。没有别的了。也许你指的是其他东西。在两个按钮中,你都在启动和停止服务,这就是为什么音乐从一开始就播放。这就像你在重新设置音乐。我该怎么做?我想会更好,是的。我按照建议做了,但现在我犯了一系列错误:1。无法解析符号设置操作。2.未知类:“Build.VERSION.SDK\u INT”。3.无法解析符号“长度”和许多其他符号。我将更好地记录自己,并尝试自己解决。感谢您提供的所有帮助。for无法解析符号“length”,只需删除变量,因为int这些不是错误,只需导入所需的库即可。@DiligentPD我已编辑了答案,