/** Register the RemoteControlClient */ private void registerRemoteControlClient() { // Request AudioFocus, so the widget is shown on the lock-screen mAudioManager.requestAudioFocus( mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mAudioManager.registerMediaButtonEventReceiver(mClementineMediaButtonEventReceiver); // Create the intent Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mClementineMediaButtonEventReceiver); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(App.mApp.getApplicationContext(), 0, mediaButtonIntent, 0); // Create the client mRcClient = new RemoteControlClient(mediaPendingIntent); if (App.mClementine.getState() == Clementine.State.PLAY) { mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } else { mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); } mRcClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE); mAudioManager.registerRemoteControlClient(mRcClient); }
/** * Set up the remote control and tell the system we want to be the default receiver for the MEDIA * buttons * * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html */ @TargetApi(14) public void setUpRemoteControlClient() { Context context = VLCApplication.getAppContext(); AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE); if (Util.isICSOrLater()) { audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent); if (mRemoteControlClient == null) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0); // create and register the remote control client mRemoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(mRemoteControlClient); } mRemoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP); } else if (Util.isFroyoOrLater()) { audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent); } }
public void setPlaybackState(final int state) { if (mRemoteControl == null) { return; } mRemoteControl.setPlaybackState(state); }
public void register(final Context context, final ComponentName mediaButtonReceiverComponent) { downloadService = (DownloadService) context; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); // build the PendingIntent for the remote control client Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0); // create and register the remote control client mRemoteControl = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(mRemoteControl); mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); mRemoteControl.setTransportControlFlags(getTransportFlags()); imageLoader = SubsonicActivity.getStaticImageLoader(context); }
private void setImage(RemoteControlClient remoteControl, Drawable drawable) { if (remoteControl != null && drawable != null) { Bitmap origBitmap = ((BitmapDrawable) drawable).getBitmap(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { origBitmap = origBitmap.copy(origBitmap.getConfig(), false); } if (origBitmap != null && !origBitmap.isRecycled()) { remoteControl .editMetadata(false) .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, origBitmap) .apply(); } else { Log.e(TAG, "Tried to load a recycled bitmap."); remoteControl .editMetadata(false) .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, null) .apply(); } } }
public void updateMetadata(final Context context, final MusicDirectory.Entry currentSong) { if (mRemoteControl == null) { return; } if (imageLoader == null) { imageLoader = SubsonicActivity.getStaticImageLoader(context); } // Update the remote controls RemoteControlClient.MetadataEditor editor = mRemoteControl.editMetadata(true); updateMetadata(currentSong, editor); editor.apply(); if (currentSong == null || imageLoader == null) { mRemoteControl .editMetadata(true) .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, null) .apply(); } else { imageLoader.loadImage(context, mRemoteControl, currentSong); } }
@SuppressLint("NewApi") @Override public void onDestroy() { super.onDestroy(); if (remoteControlClient != null) { RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true); metadataEditor.clear(); metadataEditor.apply(); audioManager.unregisterRemoteControlClient(remoteControlClient); } NotificationCenter.getInstance() .removeObserver(this, NotificationCenter.audioProgressDidChanged); NotificationCenter.getInstance().removeObserver(this, NotificationCenter.audioPlayStateChanged); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) void registerRemoteControl(ComponentName rcvMedia) { mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(rcvMedia); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); remoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS); mAudioManager.registerRemoteControlClient(remoteControlClient); }
/** Update the RemoteControlClient */ private void updateRemoteControlClient() { // Update playstate if (App.mClementine.getState() == Clementine.State.PLAY) { mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } else { mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); } // Get the metadata editor if (mLastSong != null && mLastSong.getArt() != null) { RemoteControlClient.MetadataEditor editor = mRcClient.editMetadata(false); editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, mLastSong.getArt()); // The RemoteControlClients displays the following info: // METADATA_KEY_TITLE (white) - METADATA_KEY_ALBUMARTIST (grey) - METADATA_KEY_ALBUM (grey) // // So i put the metadata not in the "correct" fields to display artist, track and album // TODO: Fix it when changed in newer android versions editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mLastSong.getAlbum()); editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mLastSong.getArtist()); editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mLastSong.getTitle()); editor.apply(); } }
@TargetApi(14) private void updateRemoteControlClientMetadata() { if (!Util.isICSOrLater()) // NOP check return; if (mRemoteControlClient != null) { MetadataEditor editor = mRemoteControlClient.editMetadata(true); editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mCurrentMedia.getAlbum()); editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mCurrentMedia.getArtist()); editor.putString(MediaMetadataRetriever.METADATA_KEY_GENRE, mCurrentMedia.getGenre()); editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mCurrentMedia.getTitle()); editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, mCurrentMedia.getLength()); editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, getCover()); editor.apply(); } }
@SuppressLint("NewApi") @Override public int onStartCommand(Intent intent, int flags, int startId) { try { MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject(); if (messageObject == null) { AndroidUtilities.runOnUIThread( new Runnable() { @Override public void run() { stopSelf(); } }); return START_STICKY; } if (supportLockScreenControls) { ComponentName remoteComponentName = new ComponentName(getApplicationContext(), MusicPlayerReceiver.class.getName()); try { if (remoteControlClient == null) { audioManager.registerMediaButtonEventReceiver(remoteComponentName); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(remoteComponentName); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); audioManager.registerRemoteControlClient(remoteControlClient); } remoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); } catch (Exception e) { FileLog.e("tmessages", e); } } createNotification(messageObject); } catch (Exception e) { e.printStackTrace(); } return START_STICKY; }
@SuppressLint("NewApi") private void createNotification(MessageObject messageObject) { String songName = messageObject.getMusicTitle(); String authorName = messageObject.getMusicAuthor(); AudioInfo audioInfo = MediaController.getInstance().getAudioInfo(); RemoteViews simpleContentView = new RemoteViews( getApplicationContext().getPackageName(), R.layout.player_small_notification); RemoteViews expandedView = null; if (supportBigNotifications) { expandedView = new RemoteViews( getApplicationContext().getPackageName(), R.layout.player_big_notification); } Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class); intent.setAction("com.tmessages.openplayer"); intent.setFlags(32768); PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, 0); Notification notification = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.player) .setContentIntent(contentIntent) .setContentTitle(songName) .build(); notification.contentView = simpleContentView; if (supportBigNotifications) { notification.bigContentView = expandedView; } setListeners(simpleContentView); if (supportBigNotifications) { setListeners(expandedView); } Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null; if (albumArt != null) { notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt); if (supportBigNotifications) { notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt); } } else { notification.contentView.setImageViewResource( R.id.player_album_art, R.drawable.nocover_small); if (supportBigNotifications) { notification.bigContentView.setImageViewResource( R.id.player_album_art, R.drawable.nocover_big); } } if (MediaController.getInstance().isDownloadingCurrentMessage()) { notification.contentView.setViewVisibility(R.id.player_pause, View.GONE); notification.contentView.setViewVisibility(R.id.player_play, View.GONE); notification.contentView.setViewVisibility(R.id.player_next, View.GONE); notification.contentView.setViewVisibility(R.id.player_previous, View.GONE); notification.contentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE); if (supportBigNotifications) { notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE); notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE); notification.bigContentView.setViewVisibility(R.id.player_next, View.GONE); notification.bigContentView.setViewVisibility(R.id.player_previous, View.GONE); notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE); } } else { notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE); notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE); notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE); if (supportBigNotifications) { notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE); notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE); notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE); } if (MediaController.getInstance().isAudioPaused()) { notification.contentView.setViewVisibility(R.id.player_pause, View.GONE); notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE); if (supportBigNotifications) { notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE); notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE); } } else { notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE); notification.contentView.setViewVisibility(R.id.player_play, View.GONE); if (supportBigNotifications) { notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE); notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE); } } } notification.contentView.setTextViewText(R.id.player_song_name, songName); notification.contentView.setTextViewText(R.id.player_author_name, authorName); if (supportBigNotifications) { notification.bigContentView.setTextViewText(R.id.player_song_name, songName); notification.bigContentView.setTextViewText(R.id.player_author_name, authorName); } notification.flags |= Notification.FLAG_ONGOING_EVENT; startForeground(5, notification); if (remoteControlClient != null) { RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true); metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName); metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName); if (audioInfo != null && audioInfo.getCover() != null) { try { metadataEditor.putBitmap( RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, audioInfo.getCover()); } catch (Throwable e) { FileLog.e("tmessages", e); } } metadataEditor.apply(); } }
/** * A function to control the Remote Control Client. It is needed for compatibility with devices * below Ice Cream Sandwich (4.0). * * @param p Playback state */ @TargetApi(14) private void setRemoteControlClientPlaybackState(int p) { if (!Util.isICSOrLater()) return; if (mRemoteControlClient != null) mRemoteControlClient.setPlaybackState(p); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) void updateRemoteControl() { updateRemoteControlState(RemoteControlClient.PLAYSTATE_PLAYING); remoteControlClient.setTransportControlFlags( RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT); // Update the remote controls Bitmap bitmap = MediaUtils.getArtworkQuick(this, currentTrack, screenWidth / 2, screenWidth / 2); int redTop = 0, greenTop = 0, blueTop = 0, pixelsTop = 0; int redBtm = 0, greenBtm = 0, blueBtm = 0, pixelsBtm = 0; int colorTop = 0, colorBtm = 0; if (bitmap != null) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); for (int i = 0; i < w; i++) { try { colorTop = bitmap.getPixel(i, 0); redTop += Color.red(colorTop); greenTop += Color.green(colorTop); blueTop += Color.blue(colorTop); pixelsTop += 1; colorBtm = bitmap.getPixel(i, h - 1); redBtm += Color.red(colorBtm); greenBtm += Color.green(colorBtm); blueBtm += Color.blue(colorBtm); pixelsBtm += 1; } catch (Exception e) { } } if (pixelsTop > 0 && pixelsBtm > 0) { colorTop = Color.rgb(redTop / pixelsTop, greenTop / pixelsTop, blueTop / pixelsTop); // EDE7E9 colorBtm = Color.rgb(redBtm / pixelsBtm, greenBtm / pixelsBtm, blueBtm / pixelsBtm); Shader shader = new LinearGradient(w / 2, 0, w / 2, h, colorTop, colorBtm, Shader.TileMode.CLAMP); Bitmap bitmapBgr = Bitmap.createBitmap(w, screenHeight / 2, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmapBgr); Paint paint = new Paint(); paint.setShader(shader); canvas.drawRect(0, 0, w, screenHeight / 2, paint); canvas.drawBitmap(bitmap, 0, (screenHeight / 2 - screenWidth / 2) / 2, null); bitmap.recycle(); bitmap = bitmapBgr; } } else { // create random color bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); Random rnd = new Random(); bitmap.eraseColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); } remoteControlClient .editMetadata(true) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, currentTrack.getArtist()) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentTrack.getAlbum()) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentTrack.getTitle()) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, currentTrack.getDuration()) .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, bitmap) .apply(); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) void updateRemoteControlState(int state) { remoteControlClient.setPlaybackState(state); }