0

一段时间后,我需要在我的应用中暂停音频播放。我正在使用OneTimeWorkRequest

OneTimeWorkRequest sleepTimerWorkRequest = new OneTimeWorkRequest.Builder(SleepTimer.class)
        .setInitialDelay(5, TimeUnit.MINUTES)
        .build();

WorkManager.getInstance(this).enqueue(sleepTimerWorkRequest);

我正在使用MediaBrowserServiceCompat课程来播放音频,但我不确定如何暂停Worker课程中的音频。我尝试做这样的事情,但它不起作用:

public class SleepTimer extends Worker {

    private static WeakReference<Context> mContext;

    public SleepTimer(@NonNull Context context, @NonNull WorkerParameters params) {
        super(context, params);
        mContext = new WeakReference<>(context);
    }

    @NonNull
    @Override
    public Result doWork() {

        MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(getApplicationContext(), MediaPlayerService.class.getSimpleName());
        mediaSessionCompat.getController().getTransportControls().pause();

        return Result.success();
    }
}
4

0 回答 0