@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); }
@Override public void onDestroy() { super.onDestroy(); if (remoteControlClient != null) { RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true); metadataEditor.clear(); metadataEditor.apply(); audioManager.unregisterRemoteControlClient(remoteControlClient); } try { TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); } } catch (Exception e) { Log.e("tmessages", e.toString()); } NotificationManager.getInstance().removeObserver(this, NotificationManager.audioProgressDidChanged); NotificationManager.getInstance().removeObserver(this, NotificationManager.audioPlayStateChanged); }
private void playbackStateUpdate(int state) { boolean active; switch (state) { case RemoteControlClient.PLAYSTATE_PLAYING: active = true; break; case RemoteControlClient.PLAYSTATE_ERROR: case RemoteControlClient.PLAYSTATE_PAUSED: default: active = false; break; } if (active != mActive) { mActive = active; refreshState(); if (DEBUG) log(getKey() + ": playbackStateUpdate("+state+")"); } }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void registrér() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) return; App.audioManager.registerMediaButtonEventReceiver(fjernbetjeningReciever); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) return; // 'det er irriterende at den ændre billedet på lock - screen, det skal være muligt at disable dette.' if (!App.prefs.getBoolean("fjernbetjening", true)) return; Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON).setComponent(fjernbetjeningReciever); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(ApplicationSingleton.instans, 0, mediaButtonIntent, 0); // create and register the remote control client remoteControlClient = new RemoteControlClient(mediaPendingIntent); remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_STOP | RemoteControlClient.FLAG_KEY_MEDIA_PLAY ); App.audioManager.registerRemoteControlClient(remoteControlClient); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void registerRemoteControlClient(ComponentName componentName) { audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); remoteControlIntent.setComponent(componentName); RemoteControlClient localRemoteControlClient = new RemoteControlClient( PendingIntent.getBroadcast(context, 0, remoteControlIntent, 0)); localRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE ); audioManager.registerRemoteControlClient(localRemoteControlClient); this.remoteControlClient = localRemoteControlClient; }
/** * Perform initialization required for RemoteControlClient. * * @param context A context to use. * @param am The AudioManager service. */ public static void registerRemote(Context context, AudioManager am) { if (!MediaButtonReceiver.useHeadsetControls(context)) { // RemoteControlClient requires MEDIA_BUTTON intent return; } MediaButtonReceiver.registerMediaButton(context); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(new ComponentName(context.getPackageName(), MediaButtonReceiver.class.getName())); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0); RemoteControlClient remote = new RemoteControlClient(mediaPendingIntent); int flags = RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE; remote.setTransportControlFlags(flags); am.registerRemoteControlClient(remote); sRemote = remote; }
/** * Update the remote with new metadata. * {@link #registerRemote(Context, AudioManager)} must have been called * first. * * @param context A context to use. * @param song The song containing the new metadata. * @param state PlaybackService state, used to determine playback state. */ public static void updateRemote(Context context, Song song, int state) { RemoteControlClient remote = sRemote; if (remote == null) return; remote.setPlaybackState((state & PlaybackService.FLAG_PLAYING) != 0 ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED); RemoteControlClient.MetadataEditor editor = remote.editMetadata(true); if (song != null) { editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, song.artist); editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, song.album); editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, song.title); Bitmap bitmap = song.getCover(context); if (bitmap != null) { // Create a copy of the cover art, since RemoteControlClient likes // to recycle what we give it. bitmap = bitmap.copy(Bitmap.Config.RGB_565, false); } editor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, bitmap); } editor.apply(); }
/** * A function to control the Remote Control Client. It is needed for * compatibility with devices below Ice Cream Sandwich (4.0). * * @param state Playback state */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void setRemoteControlClientPlaybackState(int state) { if (!AndroidUtil.isICSOrLater() || mRemoteControlClient == null) return; switch (state) { case MediaPlayer.Event.Playing: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); break; case MediaPlayer.Event.Paused: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); break; case MediaPlayer.Event.Stopped: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); break; } }
/** * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void changeRemoteControlClient(AudioManager am, boolean acquire) { if (acquire) { Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); // create and register the remote control client mRemoteControlClient = new RemoteControlClient(mediaPendingIntent); am.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 { am.unregisterRemoteControlClient(mRemoteControlClient); mRemoteControlClient = null; } }
/** * Propagate the playback state to the media session and the lock screen remote control. * <p/> * See also : * {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_STOPPED} * {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_PLAYING} * {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_PAUSED} * * @param state playback state. */ @SuppressWarnings("deprecation") public void setPlaybackState(int state) { switch (state) { case PLAYBACK_STATE_STOPPED: setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_STOPPED); mMediaSession.setActive(false); break; case PLAYBACK_STATE_PLAYING: setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_PLAYING); mMediaSession.setActive(true); break; case PLAYBACK_STATE_PAUSED: setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_PAUSED); break; default: Log.e(TAG, "Unknown playback state."); break; } }
/** * A map between {@link android.media.RemoteControlClient} flags and {@link android.media.session.PlaybackState} actions. * @return The value to provide for {@link android.media.session.PlaybackState} for actions. */ private static long getPlaybackStateActions(final int transportControlFlags) { final Map<Integer, Long> FLAG_MAP = new HashMap<>(); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_STOP, PlaybackStateCompat.ACTION_STOP); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_NEXT, PlaybackStateCompat.ACTION_SKIP_TO_NEXT); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PAUSE, PlaybackStateCompat.ACTION_PAUSE); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD, PlaybackStateCompat.ACTION_FAST_FORWARD); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_REWIND, PlaybackStateCompat.ACTION_REWIND); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY, PlaybackStateCompat.ACTION_PLAY); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE, PlaybackStateCompat.ACTION_PLAY_PAUSE); FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_RATING, PlaybackStateCompat.ACTION_SET_RATING); long actions = 0; for (Map.Entry<Integer, Long> flags : FLAG_MAP.entrySet()) { if ((transportControlFlags & flags.getKey()) == flags.getKey()) { if (actions == 0) actions = flags.getValue(); else actions |= flags.getValue(); } } return actions; }
static int getStateFromPlayState(PlayState playState) { switch (playState) { case BUFFERING: return RemoteControlClient.PLAYSTATE_BUFFERING; case ERROR: return RemoteControlClient.PLAYSTATE_ERROR; case FAST_FORWARDING: return RemoteControlClient.PLAYSTATE_FAST_FORWARDING; case PAUSED: return RemoteControlClient.PLAYSTATE_PAUSED; case PLAYING: return RemoteControlClient.PLAYSTATE_PLAYING; case REWINDING: return RemoteControlClient.PLAYSTATE_REWINDING; case SKIPPING_BACKWARDS: return RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS; case SKIPPING_FORWARDS: return RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS; case STOPPED: return RemoteControlClient.PLAYSTATE_STOPPED; default: return RemoteControlClient.PLAYSTATE_ERROR; } }
public boolean wasPlayingRecently() { switch (mState) { case RemoteControlClient.PLAYSTATE_PLAYING: case RemoteControlClient.PLAYSTATE_FAST_FORWARDING: case RemoteControlClient.PLAYSTATE_REWINDING: case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS: case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS: case RemoteControlClient.PLAYSTATE_BUFFERING: // actively playing or about to play return true; case RemoteControlClient.PLAYSTATE_STOPPED: case RemoteControlClient.PLAYSTATE_PAUSED: case RemoteControlClient.PLAYSTATE_ERROR: return ((SystemClock.elapsedRealtime() - mStateChangeTimeMs) < DISPLAY_TIMEOUT_MS); default: LOGE("PlaybackInfo", "Unknown playback state " + mState + " in wasPlayingRecently()"); return false; } }
public void a(boolean flag, long l1, int i1) { if (m != null) { RemoteControlClient remotecontrolclient = m; byte byte0; float f1; if (flag) { byte0 = 3; } else { byte0 = 1; } if (flag) { f1 = 1.0F; } else { f1 = 0.0F; } remotecontrolclient.setPlaybackState(byte0, l1, f1); m.setTransportControlFlags(i1); } }
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); }
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) { updateAlbumArt(currentSong, null); } else { imageLoader.loadImage(context, this, currentSong); } }
@Override public void register(final Context context, final ComponentName mediaButtonReceiverComponent) { super.register(context, mediaButtonReceiverComponent); mRemoteControl.setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() { @Override public long onGetPlaybackPosition() { return downloadService.getPlayerPosition(); } }); mRemoteControl.setPlaybackPositionUpdateListener(new RemoteControlClient.OnPlaybackPositionUpdateListener() { @Override public void onPlaybackPositionUpdate(final long newPosition) { new SilentBackgroundTask<Void>(context) { @Override protected Void doInBackground() throws Throwable { downloadService.seekTo((int) newPosition); return null; } }.execute(); setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING, 0, 0); } }); }
@Override public void onCompletion(MediaPlayer mp) { playerStatus = STATUS_STOPPED; if (nowPlayingPosition < (songArray.size() - 1)) { Logging.Log(LOG_TAG,"PlaybackService completed, playing next song"); next(); } else { Logging.Log(LOG_TAG,"PlaybackService completed, end of playlist"); playerStatus = STATUS_STOPPED; notificationManager.cancel(NOTIFICATION_ID); if (updateProgressThread != null) { updateProgressThread.interrupt(); try { updateProgressThread.join(500); } catch (InterruptedException e) { } } notificationManager.cancel(NOTIFICATION_ID); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); releaseResources(); sendStateChangeBroadcast(); } }
private void stopPlayer() { mediaPlayer.stop(); mediaPlayer.reset(); playerStatus = STATUS_STOPPED; if (updateProgressThread != null) { updateProgressThread.interrupt(); try { updateProgressThread.join(500); } catch (InterruptedException e) { } } notificationManager.cancel(NOTIFICATION_ID); remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); releaseResources(); sendStateChangeBroadcast(); }
static void addNewMetadata(Bundle bundle, android.media.RemoteControlClient.MetadataEditor metadataeditor) { if (bundle != null) { if (bundle.containsKey("android.media.metadata.YEAR")) { metadataeditor.putLong(8, bundle.getLong("android.media.metadata.YEAR")); } if (bundle.containsKey("android.media.metadata.RATING")) { metadataeditor.putObject(101, bundle.getParcelable("android.media.metadata.RATING")); } if (bundle.containsKey("android.media.metadata.USER_RATING")) { metadataeditor.putObject(0x10000001, bundle.getParcelable("android.media.metadata.USER_RATING")); return; } } }
public void refreshState(boolean flag, long l, int i) { if (mRemoteControl != null) { RemoteControlClient remotecontrolclient = mRemoteControl; float f; byte byte0; if (flag) { byte0 = 3; } else { byte0 = 1; } if (flag) { f = 1.0F; } else { f = 0.0F; } remotecontrolclient.setPlaybackState(byte0, l, f); mRemoteControl.setTransportControlFlags(i); } }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private static void broadcastStateChangeToRemoteControl(String title, boolean preparing, boolean titleOrSongHaveChanged) { try { if (localSong == null) { remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); } else { remoteControlClient.setPlaybackState(preparing ? RemoteControlClient.PLAYSTATE_BUFFERING : (playing ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED)); if (titleOrSongHaveChanged) { final RemoteControlClient.MetadataEditor ed = remoteControlClient.editMetadata(true); ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title); ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, localSong.artist); ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, localSong.album); ed.putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, localSong.track); ed.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, localSong.lengthMS); //Oh!!!! METADATA_KEY_YEAR is only handled in API 19+ !!! :( //http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/media/MediaMetadataEditor.java#MediaMetadataEditor.0METADATA_KEYS_TYPE //http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/media/RemoteControlClient.java#RemoteControlClient.0METADATA_KEYS_TYPE_LONG if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) ed.putLong(MediaMetadataRetriever.METADATA_KEY_YEAR, localSong.year); ed.apply(); } } } catch (Throwable ex) { ex.printStackTrace(); } }
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 = SubsonicTabActivity.getStaticImageLoader(context); }
public void updateMetadata(final Context context, final MusicDirectory.Entry currentSong) { if(imageLoader == null) { imageLoader = SubsonicTabActivity.getStaticImageLoader(context); } // Update the remote controls mRemoteControl.editMetadata(true) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, (currentSong == null) ? null : currentSong.getArtist()) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, (currentSong == null) ? null : currentSong.getAlbum()) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, (currentSong == null) ? null : currentSong.getArtist()) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, (currentSong) == null ? null : currentSong.getTitle()) .putString(MediaMetadataRetriever.METADATA_KEY_GENRE, (currentSong) == null ? null : currentSong.getGenre()) .putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, (currentSong == null) ? 0 : ((currentSong.getTrack() == null) ? 0 : currentSong.getTrack())) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, (currentSong == null) ? 0 : ((currentSong.getDuration() == null) ? 0 : currentSong.getDuration())) .apply(); if (currentSong == null || imageLoader == null) { mRemoteControl.editMetadata(true) .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, null) .apply(); } else { imageLoader.loadImage(context, mRemoteControl, currentSong); } }
@Override public void register(final Context context, final ComponentName mediaButtonReceiverComponent) { super.register(context, mediaButtonReceiverComponent); mRemoteControl.setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() { @Override public long onGetPlaybackPosition() { return downloadService.getPlayerPosition(); } }); mRemoteControl.setPlaybackPositionUpdateListener(new RemoteControlClient.OnPlaybackPositionUpdateListener() { @Override public void onPlaybackPositionUpdate(long newPosition) { downloadService.seekTo((int) newPosition); } }); }
public void loadImage(Context context, RemoteControlClient remoteControl, MusicDirectory.Entry entry) { if (entry == null || entry.getCoverArt() == null) { setUnknownImage(remoteControl); return; } Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), imageSizeLarge)); if (bitmap != null) { Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap); setImage(remoteControl, drawable); return; } setUnknownImage(remoteControl); queue.offer(new Task(context, entry, imageSizeLarge, imageSizeLarge, false, new RemoteControlClientTaskHandler(remoteControl))); }
@SuppressLint("NewApi") private RemoteControlClient setupRemoteControlClient() { if (Build.VERSION.SDK_INT < 14) { return null; } Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(new ComponentName(getPackageName(), MediaButtonReceiver.class.getName())); PendingIntent mediaPendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, mediaButtonIntent, 0); remoteControlClient = new RemoteControlClient(mediaPendingIntent); int controlFlags; if (android.os.Build.VERSION.SDK_INT < 16) { controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT; } else { controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE; } remoteControlClient.setTransportControlFlags(controlFlags); return remoteControlClient; }
/** * Jumps to the previous song on the list. * * @note Remember to call `playSong()` to make the MusicPlayer * actually play the music. */ public void previous(boolean userSkippedSong) { if (serviceState != ServiceState.Paused && serviceState != ServiceState.Playing) return; if (userSkippedSong) broadcastState(ServicePlayMusic.BROADCAST_EXTRA_SKIP_PREVIOUS); // Updates Lock-Screen Widget if (lockscreenController != null) lockscreenController.setPlaybackState(RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS); currentSongPosition--; if (currentSongPosition < 0) currentSongPosition = songs.size() - 1; }
@Override public void register(final Context context, final ComponentName mediaButtonReceiverComponent) { super.register(context, mediaButtonReceiverComponent); mRemoteControl.setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() { @Override public long onGetPlaybackPosition() { return downloadService.getPlayerPosition(); } }); mRemoteControl.setPlaybackPositionUpdateListener(new RemoteControlClient.OnPlaybackPositionUpdateListener() { @Override public void onPlaybackPositionUpdate(long newPosition) { downloadService.seekTo((int) newPosition); setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } }); }
public void loadImage(Context context, RemoteControlClient remoteControl, MusicDirectory.Entry entry) { if (entry == null || entry.getCoverArt() == null) { setUnknownImage(remoteControl); return; } Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), imageSizeLarge)); if (bitmap != null && !bitmap.isRecycled()) { Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap); setImage(remoteControl, drawable); return; } setUnknownImage(remoteControl); queue.offer(new Task(context, entry, imageSizeLarge, imageSizeLarge, false, new RemoteControlClientTaskHandler(remoteControl))); }
/** * 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(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void setRemoteControlClientPlaybackState(int state) { if (!LibVlcUtil.isICSOrLater() || mRemoteControlClient == null) return; switch (state) { case EventHandler.MediaPlayerPlaying: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); break; case EventHandler.MediaPlayerPaused: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); break; case EventHandler.MediaPlayerStopped: mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); break; } }
@Override void setup() { super.setup(); mRemoteControlClient.setOnGetPlaybackPositionListener( new RemoteControlClient.OnGetPlaybackPositionListener() { @Override public long onGetPlaybackPosition() { return mService.position(); } }); mRemoteControlClient.setPlaybackPositionUpdateListener( new RemoteControlClient.OnPlaybackPositionUpdateListener() { @Override public void onPlaybackPositionUpdate(long newPositionMs) { mService.seek(newPositionMs); } }); }
private void refreshRemoteControlClient(int playstate) { if (playerManager.isRunning()) { remoteControlClient.setPlaybackState(playstate); RemoteControlClient.MetadataEditor editor = remoteControlClient.editMetadata(false); AbstractMediaSource ams = playerManager.getCurrentMediaSource(); editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, ams.getTitle()); editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, ams.getSummary()); //editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, ams.getDuration()); //editor.putLong(MediaMetadataRetriever.METADATA_KEY_LOCATION, ams.getPosition()); editor.apply(); bluetoothNotifyChange(ams, AVRCP_ACTION_PLAYER_STATUS_CHANGED); bluetoothNotifyChange(ams, AVRCP_ACTION_META_CHANGED); } }
/** * Initializes the remote control client */ private void setUpRemoteControlClient() { if (mEnableLockscreenControls) { if (mRemoteControlClientCompat == null) { final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mMediaButtonReceiverComponent); mRemoteControlClientCompat = new RemoteControlClientCompat( PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT)); RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat); } // Flags for the media transport control that this client supports. final int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP; mRemoteControlClientCompat.setTransportControlFlags(flags); } }
/** * Updates the lockscreen controls, if enabled. * * @param what The broadcast */ private void updateRemoteControlClient(final String what) { if (mEnableLockscreenControls && mRemoteControlClientCompat != null) { if (what.equals(PLAYSTATE_CHANGED)) { // If the playstate change notify the lock screen // controls mRemoteControlClientCompat .setPlaybackState(mIsSupposedToBePlaying ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED); } else if (what.equals(META_CHANGED)) { // Update the ockscreen controls mRemoteControlClientCompat .editMetadata(true) .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, getArtistName()) .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, getAlbumName()) .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getTrackName()) .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration()) .putBitmap( RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, getAlbumArt()).apply(); } } }