Android How to implement a MediaPlayer on lockscreen API19 <=API28
I am currently developing an app that uses audio.
When the device is locked, the user must be able to control the audio on the lock screen. This must work on API 19 (4.4 KitKat) until API28 (pie, with oreo notification channel) (and up). The app must be able to receive input (button listeners/clicks on the buttons) and the app must be able to hand over the right song to the service to play.
I am trying to make something like this or an album lock screen like Android 4.4 as a fallback.
I successfully managed to make a notification in the notification drawer with MediaPlayer controls.
Now, I try something similar, but this time, I want to control the audio on the lock screen.
I already managed to make this but it only works on API21 to API25.
It needs to work on API 19 until 26 (and newer/future proof).
And here is my code:
public class LockscreenService extends Service {
private MediaSession mSession;
public static final String ACTION_PLAY = "ACTION_PLAY";
public static final String ACTION_PAUSE = "ACTION_PAUSE";
public static final String ACTION_REWIND = "ACTION_REWIND";
public static final String ACTION_FAST_FORWARD = "ACTION_FAST_FORWARD";
public static final String ACTION_NEXT = "ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
public static final String ACTION_STOP = "ACTION_STOP";
private MediaPlayer mMediaplayer;
private MediaSessionManager mediaSessionManager;
private MediaController mController;
//region EXTENDS
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public boolean onUnbind(Intent intent)
// mSession.release();
return super.onUnbind(intent);
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
if (mediaSessionManager == null)
initMediaSession();
handleIntent(intent);
return super.onStartCommand(intent, flags, startId);
//endregion
//region
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession()
mSession = new MediaSession(getApplicationContext(), "example session");
mController = new MediaController(getApplicationContext(), mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback()
@Override
public void onPlay()
super.onPlay();
playMusic();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onPause()
super.onPause();
pauseMusic();
buildNotification(generateAction(android.R.drawable.ic_media_play, "pause", ACTION_PLAY));
@Override
public void onSkipToNext()
super.onSkipToNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onFastForward()
pauseMusic();
super.onSkipToNext();
@Override
public void onRewind()
pauseMusic();
super.onSkipToNext();
@Override
public void onSkipToPrevious()
super.onSkipToPrevious();
pauseMusic();
@Override
public void onStop()
super.onStop();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
stopService(intent);
);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleIntent(Intent intent) intent.getAction() == null)
// STOP
String action = Objects.requireNonNull(intent).getAction();
assert action != null;
if (action.equalsIgnoreCase(ACTION_PLAY))
Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();
mController.getTransportControls().play();
else if (action.equalsIgnoreCase(ACTION_PAUSE))
Toast.makeText(this, "Clicked pause", Toast.LENGTH_SHORT).show();
mController.getTransportControls().pause();
else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD))
Toast.makeText(this, "Clicked fast forward", Toast.LENGTH_SHORT).show();
mController.getTransportControls().fastForward();
else if (action.equalsIgnoreCase(ACTION_REWIND))
Toast.makeText(this, "Clicked rewind", Toast.LENGTH_SHORT).show();
mController.getTransportControls().rewind();
else if (action.equalsIgnoreCase(ACTION_PREVIOUS))
Toast.makeText(this, "Clicked previous", Toast.LENGTH_SHORT).show();
pauseMusic();
mController.getTransportControls().skipToPrevious();
else if (action.equalsIgnoreCase(ACTION_NEXT))
Toast.makeText(this, "Clicked next", Toast.LENGTH_SHORT).show();
mController.getTransportControls().skipToNext();
else if (action.equalsIgnoreCase(ACTION_STOP))
Toast.makeText(this, "Clicked stop", Toast.LENGTH_SHORT).show();
mController.getTransportControls().stop();
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private Notification.Action generateAction(int icon, String title, String intentAction)
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
return new Notification.Action.Builder(icon, title, pendingIntent).build();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void buildNotification(Notification.Action action)
Notification.MediaStyle style = new Notification.MediaStyle();
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Lockscreen Book Example")
.setContentText("Title of the book")
.setDeleteIntent(pendingIntent)
.setStyle(style);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "previous", ACTION_PREVIOUS));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "previous", ACTION_PREVIOUS));
builder.addAction(action);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "fast forward", ACTION_FAST_FORWARD));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "Next", ACTION_NEXT));
style.setShowActionsInCompactView(0, 1, 2, 3, 4);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
//region music controls
public void initMusic()
//getting systems default ringtone
mMediaplayer = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
public void playMusic()
if (mMediaplayer != null)
mMediaplayer.start();
else
initMusic();
//staring the player
mMediaplayer.start();
public void pauseMusic()
if (mMediaplayer != null)
mMediaplayer.pause();
//endregion
//endregion
When I try to use NotificationCompat, I can't let MediaStyle work, so I unable to generate the controls.
Can someone please help me out how I can make audio controls on the lock screen that works on KitKat until Pie (and newer)?
java android android-intent android-service android-mediaplayer
add a comment |
I am currently developing an app that uses audio.
When the device is locked, the user must be able to control the audio on the lock screen. This must work on API 19 (4.4 KitKat) until API28 (pie, with oreo notification channel) (and up). The app must be able to receive input (button listeners/clicks on the buttons) and the app must be able to hand over the right song to the service to play.
I am trying to make something like this or an album lock screen like Android 4.4 as a fallback.
I successfully managed to make a notification in the notification drawer with MediaPlayer controls.
Now, I try something similar, but this time, I want to control the audio on the lock screen.
I already managed to make this but it only works on API21 to API25.
It needs to work on API 19 until 26 (and newer/future proof).
And here is my code:
public class LockscreenService extends Service {
private MediaSession mSession;
public static final String ACTION_PLAY = "ACTION_PLAY";
public static final String ACTION_PAUSE = "ACTION_PAUSE";
public static final String ACTION_REWIND = "ACTION_REWIND";
public static final String ACTION_FAST_FORWARD = "ACTION_FAST_FORWARD";
public static final String ACTION_NEXT = "ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
public static final String ACTION_STOP = "ACTION_STOP";
private MediaPlayer mMediaplayer;
private MediaSessionManager mediaSessionManager;
private MediaController mController;
//region EXTENDS
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public boolean onUnbind(Intent intent)
// mSession.release();
return super.onUnbind(intent);
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
if (mediaSessionManager == null)
initMediaSession();
handleIntent(intent);
return super.onStartCommand(intent, flags, startId);
//endregion
//region
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession()
mSession = new MediaSession(getApplicationContext(), "example session");
mController = new MediaController(getApplicationContext(), mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback()
@Override
public void onPlay()
super.onPlay();
playMusic();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onPause()
super.onPause();
pauseMusic();
buildNotification(generateAction(android.R.drawable.ic_media_play, "pause", ACTION_PLAY));
@Override
public void onSkipToNext()
super.onSkipToNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onFastForward()
pauseMusic();
super.onSkipToNext();
@Override
public void onRewind()
pauseMusic();
super.onSkipToNext();
@Override
public void onSkipToPrevious()
super.onSkipToPrevious();
pauseMusic();
@Override
public void onStop()
super.onStop();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
stopService(intent);
);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleIntent(Intent intent) intent.getAction() == null)
// STOP
String action = Objects.requireNonNull(intent).getAction();
assert action != null;
if (action.equalsIgnoreCase(ACTION_PLAY))
Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();
mController.getTransportControls().play();
else if (action.equalsIgnoreCase(ACTION_PAUSE))
Toast.makeText(this, "Clicked pause", Toast.LENGTH_SHORT).show();
mController.getTransportControls().pause();
else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD))
Toast.makeText(this, "Clicked fast forward", Toast.LENGTH_SHORT).show();
mController.getTransportControls().fastForward();
else if (action.equalsIgnoreCase(ACTION_REWIND))
Toast.makeText(this, "Clicked rewind", Toast.LENGTH_SHORT).show();
mController.getTransportControls().rewind();
else if (action.equalsIgnoreCase(ACTION_PREVIOUS))
Toast.makeText(this, "Clicked previous", Toast.LENGTH_SHORT).show();
pauseMusic();
mController.getTransportControls().skipToPrevious();
else if (action.equalsIgnoreCase(ACTION_NEXT))
Toast.makeText(this, "Clicked next", Toast.LENGTH_SHORT).show();
mController.getTransportControls().skipToNext();
else if (action.equalsIgnoreCase(ACTION_STOP))
Toast.makeText(this, "Clicked stop", Toast.LENGTH_SHORT).show();
mController.getTransportControls().stop();
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private Notification.Action generateAction(int icon, String title, String intentAction)
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
return new Notification.Action.Builder(icon, title, pendingIntent).build();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void buildNotification(Notification.Action action)
Notification.MediaStyle style = new Notification.MediaStyle();
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Lockscreen Book Example")
.setContentText("Title of the book")
.setDeleteIntent(pendingIntent)
.setStyle(style);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "previous", ACTION_PREVIOUS));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "previous", ACTION_PREVIOUS));
builder.addAction(action);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "fast forward", ACTION_FAST_FORWARD));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "Next", ACTION_NEXT));
style.setShowActionsInCompactView(0, 1, 2, 3, 4);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
//region music controls
public void initMusic()
//getting systems default ringtone
mMediaplayer = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
public void playMusic()
if (mMediaplayer != null)
mMediaplayer.start();
else
initMusic();
//staring the player
mMediaplayer.start();
public void pauseMusic()
if (mMediaplayer != null)
mMediaplayer.pause();
//endregion
//endregion
When I try to use NotificationCompat, I can't let MediaStyle work, so I unable to generate the controls.
Can someone please help me out how I can make audio controls on the lock screen that works on KitKat until Pie (and newer)?
java android android-intent android-service android-mediaplayer
did you solve it?
– kh3e
Nov 19 '18 at 17:24
add a comment |
I am currently developing an app that uses audio.
When the device is locked, the user must be able to control the audio on the lock screen. This must work on API 19 (4.4 KitKat) until API28 (pie, with oreo notification channel) (and up). The app must be able to receive input (button listeners/clicks on the buttons) and the app must be able to hand over the right song to the service to play.
I am trying to make something like this or an album lock screen like Android 4.4 as a fallback.
I successfully managed to make a notification in the notification drawer with MediaPlayer controls.
Now, I try something similar, but this time, I want to control the audio on the lock screen.
I already managed to make this but it only works on API21 to API25.
It needs to work on API 19 until 26 (and newer/future proof).
And here is my code:
public class LockscreenService extends Service {
private MediaSession mSession;
public static final String ACTION_PLAY = "ACTION_PLAY";
public static final String ACTION_PAUSE = "ACTION_PAUSE";
public static final String ACTION_REWIND = "ACTION_REWIND";
public static final String ACTION_FAST_FORWARD = "ACTION_FAST_FORWARD";
public static final String ACTION_NEXT = "ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
public static final String ACTION_STOP = "ACTION_STOP";
private MediaPlayer mMediaplayer;
private MediaSessionManager mediaSessionManager;
private MediaController mController;
//region EXTENDS
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public boolean onUnbind(Intent intent)
// mSession.release();
return super.onUnbind(intent);
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
if (mediaSessionManager == null)
initMediaSession();
handleIntent(intent);
return super.onStartCommand(intent, flags, startId);
//endregion
//region
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession()
mSession = new MediaSession(getApplicationContext(), "example session");
mController = new MediaController(getApplicationContext(), mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback()
@Override
public void onPlay()
super.onPlay();
playMusic();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onPause()
super.onPause();
pauseMusic();
buildNotification(generateAction(android.R.drawable.ic_media_play, "pause", ACTION_PLAY));
@Override
public void onSkipToNext()
super.onSkipToNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onFastForward()
pauseMusic();
super.onSkipToNext();
@Override
public void onRewind()
pauseMusic();
super.onSkipToNext();
@Override
public void onSkipToPrevious()
super.onSkipToPrevious();
pauseMusic();
@Override
public void onStop()
super.onStop();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
stopService(intent);
);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleIntent(Intent intent) intent.getAction() == null)
// STOP
String action = Objects.requireNonNull(intent).getAction();
assert action != null;
if (action.equalsIgnoreCase(ACTION_PLAY))
Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();
mController.getTransportControls().play();
else if (action.equalsIgnoreCase(ACTION_PAUSE))
Toast.makeText(this, "Clicked pause", Toast.LENGTH_SHORT).show();
mController.getTransportControls().pause();
else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD))
Toast.makeText(this, "Clicked fast forward", Toast.LENGTH_SHORT).show();
mController.getTransportControls().fastForward();
else if (action.equalsIgnoreCase(ACTION_REWIND))
Toast.makeText(this, "Clicked rewind", Toast.LENGTH_SHORT).show();
mController.getTransportControls().rewind();
else if (action.equalsIgnoreCase(ACTION_PREVIOUS))
Toast.makeText(this, "Clicked previous", Toast.LENGTH_SHORT).show();
pauseMusic();
mController.getTransportControls().skipToPrevious();
else if (action.equalsIgnoreCase(ACTION_NEXT))
Toast.makeText(this, "Clicked next", Toast.LENGTH_SHORT).show();
mController.getTransportControls().skipToNext();
else if (action.equalsIgnoreCase(ACTION_STOP))
Toast.makeText(this, "Clicked stop", Toast.LENGTH_SHORT).show();
mController.getTransportControls().stop();
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private Notification.Action generateAction(int icon, String title, String intentAction)
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
return new Notification.Action.Builder(icon, title, pendingIntent).build();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void buildNotification(Notification.Action action)
Notification.MediaStyle style = new Notification.MediaStyle();
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Lockscreen Book Example")
.setContentText("Title of the book")
.setDeleteIntent(pendingIntent)
.setStyle(style);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "previous", ACTION_PREVIOUS));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "previous", ACTION_PREVIOUS));
builder.addAction(action);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "fast forward", ACTION_FAST_FORWARD));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "Next", ACTION_NEXT));
style.setShowActionsInCompactView(0, 1, 2, 3, 4);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
//region music controls
public void initMusic()
//getting systems default ringtone
mMediaplayer = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
public void playMusic()
if (mMediaplayer != null)
mMediaplayer.start();
else
initMusic();
//staring the player
mMediaplayer.start();
public void pauseMusic()
if (mMediaplayer != null)
mMediaplayer.pause();
//endregion
//endregion
When I try to use NotificationCompat, I can't let MediaStyle work, so I unable to generate the controls.
Can someone please help me out how I can make audio controls on the lock screen that works on KitKat until Pie (and newer)?
java android android-intent android-service android-mediaplayer
I am currently developing an app that uses audio.
When the device is locked, the user must be able to control the audio on the lock screen. This must work on API 19 (4.4 KitKat) until API28 (pie, with oreo notification channel) (and up). The app must be able to receive input (button listeners/clicks on the buttons) and the app must be able to hand over the right song to the service to play.
I am trying to make something like this or an album lock screen like Android 4.4 as a fallback.
I successfully managed to make a notification in the notification drawer with MediaPlayer controls.
Now, I try something similar, but this time, I want to control the audio on the lock screen.
I already managed to make this but it only works on API21 to API25.
It needs to work on API 19 until 26 (and newer/future proof).
And here is my code:
public class LockscreenService extends Service {
private MediaSession mSession;
public static final String ACTION_PLAY = "ACTION_PLAY";
public static final String ACTION_PAUSE = "ACTION_PAUSE";
public static final String ACTION_REWIND = "ACTION_REWIND";
public static final String ACTION_FAST_FORWARD = "ACTION_FAST_FORWARD";
public static final String ACTION_NEXT = "ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
public static final String ACTION_STOP = "ACTION_STOP";
private MediaPlayer mMediaplayer;
private MediaSessionManager mediaSessionManager;
private MediaController mController;
//region EXTENDS
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public boolean onUnbind(Intent intent)
// mSession.release();
return super.onUnbind(intent);
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public int onStartCommand(Intent intent, int flags, int startId)
if (mediaSessionManager == null)
initMediaSession();
handleIntent(intent);
return super.onStartCommand(intent, flags, startId);
//endregion
//region
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession()
mSession = new MediaSession(getApplicationContext(), "example session");
mController = new MediaController(getApplicationContext(), mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback()
@Override
public void onPlay()
super.onPlay();
playMusic();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onPause()
super.onPause();
pauseMusic();
buildNotification(generateAction(android.R.drawable.ic_media_play, "pause", ACTION_PLAY));
@Override
public void onSkipToNext()
super.onSkipToNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "pause", ACTION_PAUSE));
@Override
public void onFastForward()
pauseMusic();
super.onSkipToNext();
@Override
public void onRewind()
pauseMusic();
super.onSkipToNext();
@Override
public void onSkipToPrevious()
super.onSkipToPrevious();
pauseMusic();
@Override
public void onStop()
super.onStop();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
stopService(intent);
);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleIntent(Intent intent) intent.getAction() == null)
// STOP
String action = Objects.requireNonNull(intent).getAction();
assert action != null;
if (action.equalsIgnoreCase(ACTION_PLAY))
Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();
mController.getTransportControls().play();
else if (action.equalsIgnoreCase(ACTION_PAUSE))
Toast.makeText(this, "Clicked pause", Toast.LENGTH_SHORT).show();
mController.getTransportControls().pause();
else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD))
Toast.makeText(this, "Clicked fast forward", Toast.LENGTH_SHORT).show();
mController.getTransportControls().fastForward();
else if (action.equalsIgnoreCase(ACTION_REWIND))
Toast.makeText(this, "Clicked rewind", Toast.LENGTH_SHORT).show();
mController.getTransportControls().rewind();
else if (action.equalsIgnoreCase(ACTION_PREVIOUS))
Toast.makeText(this, "Clicked previous", Toast.LENGTH_SHORT).show();
pauseMusic();
mController.getTransportControls().skipToPrevious();
else if (action.equalsIgnoreCase(ACTION_NEXT))
Toast.makeText(this, "Clicked next", Toast.LENGTH_SHORT).show();
mController.getTransportControls().skipToNext();
else if (action.equalsIgnoreCase(ACTION_STOP))
Toast.makeText(this, "Clicked stop", Toast.LENGTH_SHORT).show();
mController.getTransportControls().stop();
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private Notification.Action generateAction(int icon, String title, String intentAction)
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
return new Notification.Action.Builder(icon, title, pendingIntent).build();
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void buildNotification(Notification.Action action)
Notification.MediaStyle style = new Notification.MediaStyle();
Intent intent = new Intent(getApplicationContext(), LockscreenService.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("Lockscreen Book Example")
.setContentText("Title of the book")
.setDeleteIntent(pendingIntent)
.setStyle(style);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "previous", ACTION_PREVIOUS));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "previous", ACTION_PREVIOUS));
builder.addAction(action);
builder.addAction(generateAction(android.R.drawable.ic_media_previous, "fast forward", ACTION_FAST_FORWARD));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "Next", ACTION_NEXT));
style.setShowActionsInCompactView(0, 1, 2, 3, 4);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
//region music controls
public void initMusic()
//getting systems default ringtone
mMediaplayer = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
public void playMusic()
if (mMediaplayer != null)
mMediaplayer.start();
else
initMusic();
//staring the player
mMediaplayer.start();
public void pauseMusic()
if (mMediaplayer != null)
mMediaplayer.pause();
//endregion
//endregion
When I try to use NotificationCompat, I can't let MediaStyle work, so I unable to generate the controls.
Can someone please help me out how I can make audio controls on the lock screen that works on KitKat until Pie (and newer)?
java android android-intent android-service android-mediaplayer
java android android-intent android-service android-mediaplayer
edited Nov 14 '18 at 22:36
rrauenza
3,51921835
3,51921835
asked Nov 14 '18 at 19:30
appyGeekappyGeek
175
175
did you solve it?
– kh3e
Nov 19 '18 at 17:24
add a comment |
did you solve it?
– kh3e
Nov 19 '18 at 17:24
did you solve it?
– kh3e
Nov 19 '18 at 17:24
did you solve it?
– kh3e
Nov 19 '18 at 17:24
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307523%2fandroid-how-to-implement-a-mediaplayer-on-lockscreen-api19-api28%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307523%2fandroid-how-to-implement-a-mediaplayer-on-lockscreen-api19-api28%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
did you solve it?
– kh3e
Nov 19 '18 at 17:24