Usaamah Patel
Usaamah Patel

Reputation: 9

ANDROID - Create Notification w/ Media Controls on Service Start

I have a MediaPlayerService, currently started when the Play/Pause Button on a ListView item is clicked. See code below (CustomListAdapter):

Intent intent = new Intent(v.getContext(),MediaPlayerService.class);
intent.putExtra("StreamLink",audio);
activity.startService(intent);

When this service is started by the code above I want to create a Notification with a Play/Stop button. The user should be able to get out of the app, be able to stop Media Playback e.g. player.stop() and start player.start(). Also when the Notification is clicked it should return the user to the MainActivity.

The code for my MediaPlayerService.java:

public class MediaPlayerService extends Service implements MediaPlayer.OnPreparedListener {

MediaPlayer mMediaPlayer = null;
public String audioStreamLink;

public int onStartCommand(Intent intent, int flags, int startId) {

    // Get the Audio Streaming Link from the parsed JSON in the Main Activity
    audioStreamLink = intent.getStringExtra("StreamLink");

    // Instantiate MediaPlayer, set the Audio Type and acquire a wakelock, set the Media Player Data Source and Prepare.
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mMediaPlayer.setDataSource(audioStreamLink);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.prepareAsync();

    return START_STICKY;

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

/** Called when MediaPlayer is ready */
@Override
public void onPrepared(MediaPlayer player) {
    player.start();
}

@Override
public void onDestroy() {
    mMediaPlayer.stop();
    mMediaPlayer.reset();
    if (mMediaPlayer != null) mMediaPlayer.release();

}
}

Upvotes: 0

Views: 2905

Answers (1)

zeekhuge
zeekhuge

Reputation: 1594

The process of getting a notification is completely documented down there in the documents, for example, see this.

To help you go through all this huge documentation these are the points :

  • You need to create the custom notification using a NotificationCompat.Builder
  • A typical music player service would start the notification using startForeground()
  • To add clickable buttons to the notification use addAction() when building the notifications.
  • Actions in Notifications are defined by PendingIntent, its a kind of normal Intent when it comes to responding to it.
  • When a button on the notification is clicked, the onStartCommand() is triggered with the intent you specified for that button (if you configure the intent correctly).
  • Each button's intent should have a different action so that you can identify the intent when it is received.
  • Inside the onStartCommand() you can play/pause and do other operations based on this intent's action.

Some reference I would suggest you to read :

Things would have been a bit different if you were using a MediaSession to play the media.

Upvotes: 2

Related Questions