Code example for RemoteControlClient

0
	@Override 
	public void onAudioStarted() { 
		super.onAudioStarted(); 
		getRemoteControlClient().setPlaybackState(
				RemoteControlClient.PLAYSTATE_PLAYING);
		getAudioManager().registerRemoteControlClient(getRemoteControlClient()); 
	} 
 
	@Override 
	public void onAudioPaused() { 
		getRemoteControlClient().setPlaybackState(
				RemoteControlClient.PLAYSTATE_PAUSED);
	} 
 
	@Override 
	public void onDurationChanged(int duration) {
		getRemoteControlClient() 
				.editMetadata(false)
				.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration)
				.apply();
	} 
 
	@Override 
	public void onSongChanged(Song song) {
 
		if (song == null) {
			onAlbumArtChanged(null); 
			onTitleChanged(null); 
			onArtistChanged(null); 
			return; 
		} 
		if (song.getAlbumArt() != null) {
			onAlbumArtChanged(song.getAlbumArt());
		} 
 
		onTitleChanged(song.getTitle());
		onArtistChanged(song.getArtist());
	} 
 
	@Override 
	public void onAudioStopped() { 
		getRemoteControlClient().setPlaybackState(
				RemoteControlClient.PLAYSTATE_STOPPED);
		super.onAudioStopped(); 
	} 
 
	@Override 
	public void onTitleChanged(String title) {
		mTitle = title;
	} 
 
	@Override 
	public void onArtistChanged(String artist) {
		mArtist = artist;
	} 
 
	@Override 
	public void onAlbumArtChanged(Uri uri) {
		if (uri != null) {
			if (uri.getScheme().equals("android.resource") && uri.getLastPathSegment() != null) {
				mAlbumArt = BitmapFactory.decodeResource(getContext()
						.getResources(), Integer.valueOf(uri
						.getLastPathSegment()));
			} else if (uri.getScheme().equals("content")) {
				InputStream stream = null;
				try { 
					stream = getContext().getContentResolver().openInputStream(
							uri);
					mAlbumArt = BitmapFactory.decodeStream(stream);
				} catch (FileNotFoundException e) {
					mAlbumArt = null;
				} finally { 
					if (stream != null) {
						try { 
							stream.close();
						} catch (IOException e) {}
					} 
				} 
 
			} else { 
				InputStream stream = null;
				try { 
					stream = new URL(uri.toString()).openStream();
					mAlbumArt = BitmapFactory.decodeStream(stream);
				} catch (MalformedURLException e) {
					mAlbumArt = null;
				} catch (IOException e) {
					mAlbumArt = null;
				} finally { 
					if (stream != null) {
						try { 
							stream.close();
						} catch (IOException e) {}
					} 
				} 
			} 
		} else { 
			mAlbumArt = null;
		} 
		getRemoteControlClient().editMetadata(false).putBitmap(
				RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
				mAlbumArt);
	} 
 
	@Override 
	public void onTransportControlFlagsChanged(int transportControlFlags) {
		mTransportControlFlags = transportControlFlags;
		getRemoteControlClient() 
				.setTransportControlFlags(transportControlFlags);
	} 
 
	@Override 
	public void onChangesComplete() { 
		getRemoteControlClient().setTransportControlFlags(
				mTransportControlFlags);
		getRemoteControlClient() 
				.editMetadata(false)
				.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mTitle)
				.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mArtist)
				.putBitmap(
						RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
						mAlbumArt).apply();
	} 
 
	private RemoteControlClient getRemoteControlClient() {
		if (mRemoteControlClient == null) {
			Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
			mediaButtonIntent.setComponent(getEventReceiver());
			PendingIntent pendingIntent = PendingIntent.getBroadcast(
					getContext(), 0, mediaButtonIntent,
					PendingIntent.FLAG_CANCEL_CURRENT);
			mRemoteControlClient = new RemoteControlClient(pendingIntent);
		} 
		return mRemoteControlClient;
	} 
}