-
-
Save PhongHuynh93/e0007158c7f0a295b4c3db0e7101160a to your computer and use it in GitHub Desktop.
Minimal MediaSessionCompat needed to get RemoteControlClient to appear
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.remotecontrolclient; | |
import android.app.Service; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.media.AudioManager; | |
import android.os.IBinder; | |
import android.support.v4.media.MediaMetadataCompat; | |
import android.support.v4.media.session.MediaSessionCompat; | |
import android.support.v4.media.session.PlaybackStateCompat; | |
public class PlayerService extends Service { | |
private MediaSessionCompat mediaSession; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
ComponentName receiver = new ComponentName(getPackageName(), RemoteReceiver.class.getName()); | |
mediaSession = new MediaSessionCompat(this, "PlayerService", receiver, null); | |
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | | |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); | |
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() | |
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0) | |
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE) | |
.build()); | |
mediaSession.setMetadata(new MediaMetadataCompat.Builder() | |
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Test Artist") | |
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Test Album") | |
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "Test Track Name") | |
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 10000) | |
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, | |
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) | |
.build()); | |
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() { | |
@Override | |
public void onAudioFocusChange(int focusChange) { | |
// Ignore | |
} | |
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); | |
mediaSession.setActive(true); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
if (mediaSession.getController().getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) { | |
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() | |
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0.0f) | |
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build()); | |
} else { | |
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() | |
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f) | |
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build()); | |
} | |
return super.onStartCommand(intent, flags, startId); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mediaSession.release(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.remotecontrolclient; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.view.KeyEvent; | |
public class RemoteReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { | |
final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); | |
if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) { | |
switch (event.getKeyCode()) { | |
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: | |
context.startService(new Intent(context, PlayerService.class)); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment