android.media.session.MediaController Java Examples
The following examples show how to use
android.media.session.MediaController.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MediaControlLoader.java From Easer with GNU General Public License v3.0 | 8 votes |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private boolean handleOnApi21(@ValidData @NonNull MediaControlOperationData data) { int keyCode = toKeyCode(data.choice); KeyEvent keyEvent_down = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); KeyEvent keyEvent_up = new KeyEvent(KeyEvent.ACTION_UP, keyCode); ComponentName myNotificationListenerComponent = new ComponentName(context, MediaControlHelperNotificationListenerService.class); MediaSessionManager mediaSessionManager = ((MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE)); if (mediaSessionManager == null) { Logger.e("MediaSessionManager is null."); return false; } List<MediaController> activeSessions = mediaSessionManager.getActiveSessions(myNotificationListenerComponent); if (activeSessions.size() > 0) { MediaController mediaController = activeSessions.get(0); mediaController.dispatchMediaButtonEvent(keyEvent_down); mediaController.dispatchMediaButtonEvent(keyEvent_up); } return true; }
Example #2
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 6 votes |
public static MediaController getActiveMediaController() { ComponentName componentName = new ComponentName(context, NotificationListener4.class); MediaSessionManager mediaSessionManager = (MediaSessionManager)context.getSystemService(Context.MEDIA_SESSION_SERVICE); List<MediaController> mediaControllerList = null; try { mediaControllerList = mediaSessionManager.getActiveSessions(componentName); for(MediaController mediaController: mediaControllerList) { if(mediaController.getPackageName().equals(ctrlAppProcessName)) return mediaController; } } catch(Exception e) { PodEmuLog.error("MPlayback: Notification Listener permissions not granted"); } return null; }
Example #3
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 6 votes |
public void action_stop_ff_rev() { if(!isPlaying()) return; PodEmuLog.debug("PEMP: action STOP_FF_REV requested"); MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_PLAY) == PlaybackState.ACTION_PLAY) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_STOP_FF_REV)"); mediaController.getTransportControls().play(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_PLAY); } }
Example #4
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 6 votes |
@Override public void onActiveSessionsChanged(List<MediaController> controllers) { MediaController2Lollipop p = mMediaControllerRef.get(); if (p == null) return; if (p.mMediaController != null) { for (MediaController controller : controllers) { if (p.mMediaController == controller) { // Current media controller is still alive. return; } } } MediaController mc = pickBestMediaController(controllers); if (mc != null) { p.setMediaController(mc); } else { p.clearMediaController(true); } }
Example #5
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 6 votes |
public void action_skip_backward() { MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_REWIND) == PlaybackState.ACTION_REWIND) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_REWIND)"); mediaController.getTransportControls().fastForward(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_REWIND); /* if(Build.VERSION.SDK_INT >= 23) { execute_action(KeyEvent.KEYCODE_MEDIA_SKIP_BACKWARD); } */ } }
Example #6
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 6 votes |
public void action_skip_forward() { MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_FAST_FORWARD) == PlaybackState.ACTION_FAST_FORWARD) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_FAST_FORWARD)"); mediaController.getTransportControls().fastForward(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD); /* if(Build.VERSION.SDK_INT >= 23) { execute_action(KeyEvent.KEYCODE_MEDIA_SKIP_FORWARD); } */ } }
Example #7
Source File: ListenerService.java From scroball with MIT License | 6 votes |
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.startsWith("player.")) { final String packageName = key.substring(7); if (sharedPreferences.getBoolean(key, true)) { Log.d(TAG, "Player enabled, re-registering callbacks"); onActiveSessionsChanged(mediaControllers); } else { Log.d(TAG, "Player disabled, stopping any current tracking"); final Optional<MediaController> optionalController = Iterables.tryFind( mediaControllers, input -> input.getPackageName().equals(packageName)); if (optionalController.isPresent() && controllerCallbacks.containsKey(optionalController.get())) { MediaController controller = optionalController.get(); controller.unregisterCallback(controllerCallbacks.get(controller)); playbackTracker.handleSessionTermination(controller.getPackageName()); controllerCallbacks.remove(controller); } } } }
Example #8
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 6 votes |
public void action_play_pause() { PodEmuLog.debug("PEMP: action PLAY_PAUSE requested"); MediaController mediaController=getActiveMediaController(); if( mediaController!=null && mediaController.getPlaybackState()!=null && mediaController.getTransportControls()!=null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_PLAY_PAUSE) == PlaybackState.ACTION_PLAY_PAUSE) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_PLAY_PAUSE)"); if(mediaController.getPlaybackState().getState() == PlaybackState.STATE_PLAYING) mediaController.getTransportControls().pause(); else mediaController.getTransportControls().play(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE); } }
Example #9
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 6 votes |
@Override public void run(@NonNull MediaController2 mc) { final MediaController2Lollipop mcl = (MediaController2Lollipop) mc; final MediaController source = mcl.mMediaController; if (source != null && mToken.equals(source.getSessionToken())) { long now = SystemClock.elapsedRealtime(); final MediaMetadata metadata = source.getMetadata(); final PlaybackState playbackState = source.getPlaybackState(); long delta = SystemClock.elapsedRealtime() - now; Log.i(TAG, "Got the new metadata & playback state in " + delta + " millis. " + "The media controller is " + source.getPackageName()); mHandler.post(new Runnable() { @Override public void run() { mcl.updateMetadata(metadata); mcl.updatePlaybackState(playbackState); } }); } }
Example #10
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 5 votes |
public synchronized void action_next() { //int currentTrack = getCurrentPlaylist().getCurrentTrackPos(); //int trackCount = getCurrentPlaylist().getTrackCount(); //int newTrackPos = currentTrack + 1; //if (newTrackPos == trackCount) newTrackPos = 0; //PodEmuLog.debug("PEMP: action NEXT requested. newTrackPos=" + newTrackPos); PodEmuLog.debug("PEMP: action NEXT requested"); // TODO: implement repeat and shuffle //no loops? if(currentTrack == trackCount-1) return; if( shouldUpdatePosition() ) { getCurrentPlaylist().setIncrement(+1); //getCurrentPlaylist().setCurrentTrack(newTrackPos); } PodEmuLog.error("PEMP: action NEXT, time = " + System.currentTimeMillis()); MediaController mediaController=getActiveMediaController(); if( mediaController != null) // && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) == PlaybackState.ACTION_SKIP_TO_NEXT) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_SKIP_TO_NEXT)"); mediaController.getTransportControls().skipToNext(); } else { execute_action(KeyEvent.KEYCODE_MEDIA_NEXT); } }
Example #11
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
@Nullable private MediaController pickBestMediaController( @NonNull List<MediaController> list) { int mediaControllerScore = -1; MediaController mediaController = null; for (MediaController mc : list) { if (mc == null) continue; int mcScore = 0; // Check for the current state PlaybackState state = mc.getPlaybackState(); if (state != null) { switch (state.getState()) { case PlaybackState.STATE_STOPPED: case PlaybackState.STATE_ERROR: break; default: mcScore++; break; } } if (mcScore > mediaControllerScore) { mediaControllerScore = mcScore; mediaController = mc; } } return mediaController; }
Example #12
Source File: RemoteControlLollipop.java From Noyze with Apache License 2.0 | 5 votes |
private void unregister() { for (Map.Entry<MediaSession.Token, Pair<MediaController, MediaController.Callback>> entry : mControllers.entrySet()) { Pair<MediaController, MediaController.Callback> pair = entry.getValue(); pair.first.unregisterCallback(pair.second); } synchronized (mControllers) { mControllers.clear(); } }
Example #13
Source File: MusicPlayerActivity.java From android-music-player with Apache License 2.0 | 5 votes |
@Override public void onConnected() { mMediaBrowser.subscribe(mMediaBrowser.getRoot(), mSubscriptionCallback); MediaController mediaController = new MediaController( MusicPlayerActivity.this, mMediaBrowser.getSessionToken()); updatePlaybackState(mediaController.getPlaybackState()); updateMetadata(mediaController.getMetadata()); mediaController.registerCallback(mMediaControllerCallback); setMediaController(mediaController); }
Example #14
Source File: RemoteControlLollipop.java From Noyze with Apache License 2.0 | 5 votes |
private void unregister() { for (Map.Entry<MediaSession.Token, Pair<MediaController, MediaController.Callback>> entry : mControllers.entrySet()) { Pair<MediaController, MediaController.Callback> pair = entry.getValue(); pair.first.unregisterCallback(pair.second); } synchronized (mControllers) { mControllers.clear(); } }
Example #15
Source File: ListenerService.java From scroball with MIT License | 5 votes |
@Override public void onCreate() { ScroballApplication application = (ScroballApplication) getApplication(); sharedPreferences = application.getSharedPreferences(); ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); ScroballDB scroballDB = application.getScroballDB(); ScrobbleNotificationManager scrobbleNotificationManager = new ScrobbleNotificationManager(this, sharedPreferences, scroballDB); LastfmClient lastfmClient = application.getLastfmClient(); TrackLover trackLover = new TrackLover(lastfmClient, scroballDB, connectivityManager); Scrobbler scrobbler = new Scrobbler( lastfmClient, scrobbleNotificationManager, scroballDB, connectivityManager, trackLover); playbackTracker = new PlaybackTracker(scrobbleNotificationManager, scrobbler); Log.d(TAG, "NotificationListenerService started"); MediaSessionManager mediaSessionManager = (MediaSessionManager) getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE); ComponentName componentName = new ComponentName(this, this.getClass()); mediaSessionManager.addOnActiveSessionsChangedListener(this, componentName); NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver(scrobbler); IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION); this.registerReceiver(networkStateReceiver, filter); // Trigger change event with existing set of sessions. List<MediaController> initialSessions = mediaSessionManager.getActiveSessions(componentName); onActiveSessionsChanged(initialSessions); sharedPreferences.registerOnSharedPreferenceChangeListener(this); }
Example #16
Source File: MediaService.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
@Override public void onActiveSessionsChanged(List<MediaController> controllers) { if (controllers.size() > 0) { if (mMediaController != null && !controllers.get(0).getSessionToken().equals(mMediaController.getSessionToken())) { // Detach current controller mMediaController.unregisterCallback(mMediaCallback); Log.d("MediaService", "MediaController removed"); mMediaController = null; } if(mMediaController == null) { // Attach new controller mMediaController = controllers.get(0); mMediaController.registerCallback(mMediaCallback); mMediaCallback.onMetadataChanged(mMediaController.getMetadata()); if (mMediaController.getPlaybackState() != null) mMediaCallback.onPlaybackStateChanged(mMediaController.getPlaybackState()); Log.d("MediaService", "MediaController set: " + mMediaController.getPackageName()); SharedPreferences.Editor editor = mSettings.edit(); editor.putString(PREFS_MEDIA_CONTROLLER_PACKAGE, mMediaController.getPackageName()); editor.apply(); } } else { byte[] data = new byte[]{0}; mDevice.write(mediaArtistCharac, data, MediaService.this); mDevice.write(mediaAlbumCharac, data, MediaService.this); mDevice.write(mediaTitleCharac, data, MediaService.this); } }
Example #17
Source File: MediaService.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
public void sync() { mDevice.enableNotify(mediaCommandsCharac, commandsListener); try { mMediaSessionManager = (MediaSessionManager) mCtx.getSystemService(Context.MEDIA_SESSION_SERVICE); List<MediaController> controllers = mMediaSessionManager.getActiveSessions(new ComponentName(mCtx, NLService.class)); onActiveSessionsChanged(controllers); mMediaSessionManager.addOnActiveSessionsChangedListener(this, new ComponentName(mCtx, NLService.class)); } catch (SecurityException e) { Log.w("MediaService", "No Notification Access"); } }
Example #18
Source File: ScrobblerService.java From QuickLyric with GNU General Public License v3.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); sRunning = true; try { mediaControllerCallback = new MediaControllerCallback(this); MediaSessionManager.OnActiveSessionsChangedListener sessionsChangedListener = list -> mediaControllerCallback.registerActiveSessionCallback(ScrobblerService.this, list); listener = new WeakReference<>(sessionsChangedListener); MediaSessionManager manager = ((MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE)); ComponentName className = new ComponentName(getApplicationContext(), NotificationListenerService.class); manager.addOnActiveSessionsChangedListener(sessionsChangedListener, className); List<MediaController> controllers = manager.getActiveSessions(className); mediaControllerCallback.registerActiveSessionCallback(ScrobblerService.this, controllers); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { SharedPreferences current = getSharedPreferences("current_music", Context.MODE_PRIVATE); String artist = current.getString("artist", ""); String track = current.getString("track", ""); startForeground(NOTIFICATION_ID, NotificationUtil.makeNotification(this, artist, track, 0L, false, false)); } if (!controllers.isEmpty()) mediaControllerCallback.broadcastControllerState(this, controllers.get(0), null); } catch (Exception e) { e.printStackTrace(); return; } Log.d("geecko", "mediaControllerCallback registered"); }
Example #19
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
private void setMediaController(@NonNull MediaController controller) { if (DEBUG) Log.d(TAG, "Switching to \'" + controller.getPackageName() + "\' controller."); clearMediaController(true); mMediaController = controller; mMediaController.registerCallback(mCallback); // Get the new metadata and new playback state async-ly // to prevent possible ANRs. mThread.sendTask(new EventUpdateMetadata(mMediaController.getSessionToken())); }
Example #20
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 5 votes |
public void action_play() { PodEmuLog.debug("PEMP: action PLAY requested"); MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_PLAY) == PlaybackState.ACTION_PLAY) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_PLAY)"); mediaController.getTransportControls().play(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_PLAY); } }
Example #21
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 5 votes |
public void action_pause() { PodEmuLog.debug("PEMP: action PAUSE requested"); MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_PAUSE) == PlaybackState.ACTION_PAUSE) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_PAUSE)"); mediaController.getTransportControls().pause(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_PAUSE); } }
Example #22
Source File: MediaPlayback.java From PodEmu with GNU General Public License v3.0 | 5 votes |
public void action_stop() { PodEmuLog.debug("PEMP: action STOP requested"); MediaController mediaController=getActiveMediaController(); if( mediaController != null)// && (mediaController.getPlaybackState().getActions() & PlaybackState.ACTION_STOP) == PlaybackState.ACTION_STOP) { PodEmuLog.debug("PEMP: executing action through MediaController (ACTION_STOP)"); mediaController.getTransportControls().stop(); } else { PodEmuLog.debug("PEMP: executing action through KeyEvent"); execute_action(KeyEvent.KEYCODE_MEDIA_STOP); } }
Example #23
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void onStart(Object... objects) { super.onStart(); // Init a new thread. mThread = new T(this); mThread.setPriority(Thread.MIN_PRIORITY); mThread.start(); // Media session manager leaks/holds the context for too long. // Don't let it to leak the activity, better lak the whole app. final Context context = mContext.getApplicationContext(); mMediaSessionManager = (MediaSessionManager) context .getSystemService(Context.MEDIA_SESSION_SERVICE); try { mMediaSessionManager.addOnActiveSessionsChangedListener(mSessionListener, mComponent); mSessionListener.setMediaController(this); mSessionListening = true; } catch (SecurityException exception) { Log.w(TAG, "Failed to start Lollipop media controller: " + exception.getMessage()); // Try to unregister it, just it case. try { mMediaSessionManager.removeOnActiveSessionsChangedListener(mSessionListener); } catch (Exception e) { /* unused */ } finally { mMediaSessionManager = null; mSessionListening = false; } // Media controller needs notification listener service // permissions to be granted. return; } List<MediaController> controllers = mMediaSessionManager.getActiveSessions(mComponent); mSessionListener.onActiveSessionsChanged(controllers); }
Example #24
Source File: NotificationListener4.java From PodEmu with GNU General Public License v3.0 | 5 votes |
public void onActiveSessionsChanged(List<MediaController> list) { PodEmuLog.debug(TAG + ": onActiveSessionsChanged"); /* if (playerFragment.f13549e != null && playerFragment.f13549e.equals("NotificationListener")) { this.notificationListenerTMP1.handler.removeCallbacks(this.notificationListenerTMP1.runnableSessionChangeThread); this.notificationListenerTMP1.handler.postDelayed(this.notificationListenerTMP1.runnableSessionChangeThread, 2000); } */ if(notificationListenerTMP1 != null) notificationListenerTMP1.parseActiveSessions(list); }
Example #25
Source File: MusicPlayerActivity.java From io2015-codelabs with Apache License 2.0 | 5 votes |
@Override public void onConnected() { mMediaBrowser.subscribe(mMediaBrowser.getRoot(), mSubscriptionCallback); MediaController mediaController = new MediaController( MusicPlayerActivity.this, mMediaBrowser.getSessionToken()); updatePlaybackState(mediaController.getPlaybackState()); updateMetadata(mediaController.getMetadata()); mediaController.registerCallback(mMediaControllerCallback); setMediaController(mediaController); }
Example #26
Source File: MediaController2Lollipop.java From AcDisplay with GNU General Public License v2.0 | 5 votes |
/** * {@inheritDoc} */ public void sendMediaAction(int action) { if (mMediaController == null) { // Maybe somebody is waiting to start his player by // this lovely event. // TODO: Check if it works as expected. MediaController2.broadcastMediaAction(mContext, action); return; } MediaController.TransportControls controls = mMediaController.getTransportControls(); switch (action) { case ACTION_PLAY_PAUSE: if (mPlaybackState == PlaybackState.STATE_PLAYING) { controls.pause(); } else { controls.play(); } break; case ACTION_STOP: controls.stop(); break; case ACTION_SKIP_TO_NEXT: controls.skipToNext(); break; case ACTION_SKIP_TO_PREVIOUS: controls.skipToPrevious(); break; default: throw new IllegalArgumentException(); } }
Example #27
Source File: MediaControllerCompatApi21.java From adt-leanback-support with Apache License 2.0 | 4 votes |
public static void seekTo(Object controlsObj, long pos) { ((MediaController.TransportControls)controlsObj).seekTo(pos); }
Example #28
Source File: MediaControllerCompatApi21.java From adt-leanback-support with Apache License 2.0 | 4 votes |
public static void setRating(Object controlsObj, Object ratingObj) { ((MediaController.TransportControls)controlsObj).setRating((Rating)ratingObj); }
Example #29
Source File: MediaControllerCompatApi21.java From adt-leanback-support with Apache License 2.0 | 4 votes |
public static void skipToPrevious(Object controlsObj) { ((MediaController.TransportControls)controlsObj).skipToPrevious(); }
Example #30
Source File: MediaControllerCompatApi21.java From adt-leanback-support with Apache License 2.0 | 4 votes |
public static int getPlaybackType(Object volumeInfoObj) { return ((MediaController.PlaybackInfo)volumeInfoObj).getPlaybackType(); }