首页 > 解决方案 > 如何让按钮暂停记住 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();
}
}

标签: javaandroid-mediaplayerseekbar

解决方案


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

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


推荐阅读