android.media.session.MediaSession Java Examples

The following examples show how to use android.media.session.MediaSession. 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: MusicService.java    From android-music-player with Apache License 2.0 7 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // Start a new MediaSession
    mSession = new MediaSession(this, "MusicService");
    mSession.setCallback(mCallback);
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    setSessionToken(mSession.getSessionToken());

    final MediaNotificationManager mediaNotificationManager = new MediaNotificationManager(this);

    mPlayback = new PlaybackManager(this, new PlaybackManager.Callback() {
        @Override
        public void onPlaybackStatusChanged(PlaybackState state) {
            mSession.setPlaybackState(state);
            mediaNotificationManager.update(mPlayback.getCurrentMedia(), state, getSessionToken());
        }
    });
}
 
Example #2
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void updateActiveSessionListeners() {
    synchronized (mLock) {
        for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
            SessionsListenerRecord listener = mSessionsListeners.get(i);
            try {
                enforceMediaPermissions(listener.mComponentName, listener.mPid, listener.mUid,
                        listener.mUserId);
            } catch (SecurityException e) {
                Log.i(TAG, "ActiveSessionsListener " + listener.mComponentName
                        + " is no longer authorized. Disconnecting.");
                mSessionsListeners.remove(i);
                try {
                    listener.mListener
                            .onActiveSessionsChanged(new ArrayList<MediaSession.Token>());
                } catch (Exception e1) {
                    // ignore
                }
            }
        }
    }
}
 
Example #3
Source File: AvrcpService5.java    From Botifier with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mMediaSession = new MediaSession(this, TAG);
    setUpCallBack();
    mNotifications = new ArrayList<Botification>();

    final IntentFilter filter = new IntentFilter();
    filter.addAction(SERVICECMD);
    // Attach the broadcast listener
    mSharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    mHandler = new Handler(){
        public void handleMessage(Message msg){
            resetNotify(true);
        }
    };
}
 
Example #4
Source File: MusicService.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // Start a new MediaSession
    mSession = new MediaSession(this, "MusicService");
    mSession.setCallback(mCallback);
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    setSessionToken(mSession.getSessionToken());

    final MediaNotificationManager mediaNotificationManager = new MediaNotificationManager(this);

    mPlayback = new PlaybackManager(this, new PlaybackManager.Callback() {
        @Override
        public void onPlaybackStatusChanged(PlaybackState state) {
            mSession.setPlaybackState(state);
            mediaNotificationManager.update(mPlayback.getCurrentMedia(), state, getSessionToken());
        }
    });
}
 
Example #5
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void setFlags(int flags) {
    if ((flags & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) {
        int pid = getCallingPid();
        int uid = getCallingUid();
        mService.enforcePhoneStatePermission(pid, uid);
    }
    mFlags = flags;
    if ((flags & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) {
        final long token = Binder.clearCallingIdentity();
        try {
            mService.setGlobalPrioritySession(MediaSessionRecord.this);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }
    mHandler.post(MessageHandler.MSG_UPDATE_SESSION_STATE);
}
 
Example #6
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void pushAddressedPlayerChangedLocked() {
    if (mCallback == null) {
        return;
    }
    try {
        MediaSessionRecord mediaButtonSession = getMediaButtonSessionLocked();
        if (mediaButtonSession != null) {
            mCallback.onAddressedPlayerChangedToMediaSession(
                    new MediaSession.Token(mediaButtonSession.getControllerBinder()));
        } else if (mCurrentFullUserRecord.mLastMediaButtonReceiver != null) {
            mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
                    mCurrentFullUserRecord.mLastMediaButtonReceiver
                            .getIntent().getComponent());
        } else if (mCurrentFullUserRecord.mRestoredMediaButtonReceiver != null) {
            mCallback.onAddressedPlayerChangedToMediaButtonReceiver(
                    mCurrentFullUserRecord.mRestoredMediaButtonReceiver);
        }
    } catch (RemoteException e) {
        Log.w(TAG, "Failed to pushAddressedPlayerChangedLocked", e);
    }
}
 
Example #7
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public void updateSession(MediaSessionRecord record) {
    synchronized (mLock) {
        FullUserRecord user = getFullUserRecordLocked(record.getUserId());
        if (user == null) {
            Log.w(TAG, "Unknown session updated. Ignoring.");
            return;
        }
        if ((record.getFlags() & MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY) != 0) {
            if (DEBUG_KEY_EVENT) {
                Log.d(TAG, "Global priority session is updated, active=" + record.isActive());
            }
            user.pushAddressedPlayerChangedLocked();
        } else {
            if (!user.mPriorityStack.contains(record)) {
                Log.w(TAG, "Unknown session updated. Ignoring.");
                return;
            }
            user.mPriorityStack.onSessionStateChange(record);
        }
        mHandler.postSessionsChanged(record.getUserId());
    }
}
 
Example #8
Source File: MusicBrowserService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    String args[] = mediaId.split("_");
    if (args.length != 2) {
        return;
    }
    try {
        int did = Integer.parseInt(args[0]);
        int id = Integer.parseInt(args[1]);
        ArrayList<MessageObject> arrayList = musicObjects.get(did);
        ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(did);
        if (arrayList == null || id < 0 || id >= arrayList.size()) {
            return;
        }
        lastSelectedDialog = did;
        MessagesController.getNotificationsSettings(currentAccount).edit().putInt("auto_lastSelectedDialog", did).commit();
        MediaController.getInstance().setPlaylist(arrayList, arrayList.get(id), false);
        mediaSession.setQueue(arrayList1);
        if (did > 0) {
            TLRPC.User user = users.get(did);
            if (user != null) {
                mediaSession.setQueueTitle(ContactsController.formatName(user.first_name, user.last_name));
            } else {
                mediaSession.setQueueTitle("DELETED USER");
            }
        } else {
            TLRPC.Chat chat = chats.get(-did);
            if (chat != null) {
                mediaSession.setQueueTitle(chat.title);
            } else {
                mediaSession.setQueueTitle("DELETED CHAT");
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    handlePlayRequest();
}
 
Example #9
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void pushSessionsChanged(int userId) {
    synchronized (mLock) {
        FullUserRecord user = getFullUserRecordLocked(userId);
        if (user == null) {
            Log.w(TAG, "pushSessionsChanged failed. No user with id=" + userId);
            return;
        }
        List<MediaSessionRecord> records = getActiveSessionsLocked(userId);
        int size = records.size();
        ArrayList<MediaSession.Token> tokens = new ArrayList<MediaSession.Token>();
        for (int i = 0; i < size; i++) {
            tokens.add(new MediaSession.Token(records.get(i).getControllerBinder()));
        }
        pushRemoteVolumeUpdateLocked(userId);
        for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
            SessionsListenerRecord record = mSessionsListeners.get(i);
            if (record.mUserId == UserHandle.USER_ALL || record.mUserId == userId) {
                try {
                    record.mListener.onActiveSessionsChanged(tokens);
                } catch (RemoteException e) {
                    Log.w(TAG, "Dead ActiveSessionsListener in pushSessionsChanged, removing",
                            e);
                    mSessionsListeners.remove(i);
                }
            }
        }
    }
}
 
Example #11
Source File: NotificationBuilder.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public ChromeNotificationBuilder setMediaStyle(MediaSessionCompat session, int[] actions,
        PendingIntent intent, boolean showCancelButton) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Notification.MediaStyle style = new Notification.MediaStyle();
        style.setMediaSession(((MediaSession) session.getMediaSession()).getSessionToken());
        style.setShowActionsInCompactView(actions);
        mBuilder.setStyle(style);
    }
    return this;
}
 
Example #12
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
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: MusicBrowserService.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    ApplicationLoader.postInitApplication();

    lastSelectedDialog = MessagesController.getNotificationsSettings(currentAccount).getInt("auto_lastSelectedDialog", 0);

    mediaSession = new MediaSession(this, "MusicService");
    setSessionToken(mediaSession.getSessionToken());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, LaunchActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    extras.putBoolean(SLOT_RESERVATION_QUEUE, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_PREV, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_NEXT, true);
    mediaSession.setExtras(extras);

    updatePlaybackState(null);

    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidStart);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidReset);
}
 
Example #14
Source File: MusicBrowserService.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    String args[] = mediaId.split("_");
    if (args.length != 2) {
        return;
    }
    try {
        int did = Integer.parseInt(args[0]);
        int id = Integer.parseInt(args[1]);
        ArrayList<MessageObject> arrayList = musicObjects.get(did);
        ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(did);
        if (arrayList == null || id < 0 || id >= arrayList.size()) {
            return;
        }
        lastSelectedDialog = did;
        MessagesController.getNotificationsSettings(currentAccount).edit().putInt("auto_lastSelectedDialog", did).commit();
        MediaController.getInstance().setPlaylist(arrayList, arrayList.get(id), false);
        mediaSession.setQueue(arrayList1);
        if (did > 0) {
            TLRPC.User user = users.get(did);
            if (user != null) {
                mediaSession.setQueueTitle(ContactsController.formatName(user.first_name, user.last_name));
            } else {
                mediaSession.setQueueTitle("DELETED USER");
            }
        } else {
            TLRPC.Chat chat = chats.get(-did);
            if (chat != null) {
                mediaSession.setQueueTitle(chat.title);
            } else {
                mediaSession.setQueueTitle("DELETED CHAT");
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    handlePlayRequest();
}
 
Example #15
Source File: NotificationService.java    From RoMote with Apache License 2.0 5 votes vote down vote up
private void setUpMediaSession() {
    mediaSession = new MediaSession(this, TAG);
    mediaSession.setActive(true);
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setPlaybackState(
            new PlaybackState.Builder()
                    .setState(PlaybackState.STATE_PLAYING, 0L, 0F)
                    .setActions(PlaybackState.ACTION_PAUSE |
                            PlaybackState.ACTION_PLAY |
                            PlaybackState.ACTION_REWIND |
                            PlaybackState.ACTION_FAST_FORWARD)
                    .build());
    mediaSession.setMetadata(new MediaMetadata.Builder().build());
}
 
Example #16
Source File: ExoPlayerAudio.java    From ChannelSurfer with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    mSession = new MediaSession(this, "ExoPlayerAudio");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(new MediaSessionCallback());
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
}
 
Example #17
Source File: WearBrowserService.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    sInstance = this;
    mContext = this;
    mSession = new MediaSession(this, "WearBrowserService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(new MediaSessionCallback());
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

}
 
Example #18
Source File: AutoMediaBrowserService.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
private void initMediaSession() {
    mMediaSession = new MediaSession( this, "Android Auto Audio Demo" );
    mMediaSession.setActive( true );
    mMediaSession.setCallback( mMediaSessionCallback );

    mMediaSessionToken = mMediaSession.getSessionToken();
    setSessionToken( mMediaSessionToken );
}
 
Example #19
Source File: AvrcpService5.java    From Botifier with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void setUpCallBack() {
    mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mMediaSession.setCallback(new MediaSession.Callback() {
        @Override
        public void onPlay() {
            super.onPlay();
            removeNotification();
        }

        @Override
        public void onPause() {
            super.onPause();
            removeNotification();
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            showNotify(1);
        }

        @Override
        public void onSkipToPrevious() {
            super.onSkipToPrevious();
        }

        @Override
        public void onStop() {
            super.onStop();
            resetNotify(true);
            showNotify(-1);
        }
    });
}
 
Example #20
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    String args[] = mediaId.split("_");
    if (args.length != 2) {
        return;
    }
    try {
        int did = Integer.parseInt(args[0]);
        int id = Integer.parseInt(args[1]);
        ArrayList<MessageObject> arrayList = musicObjects.get(did);
        ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(did);
        if (arrayList == null || id < 0 || id >= arrayList.size()) {
            return;
        }
        lastSelectedDialog = did;
        MessagesController.getNotificationsSettings(currentAccount).edit().putInt("auto_lastSelectedDialog", did).commit();
        MediaController.getInstance().setPlaylist(arrayList, arrayList.get(id), false);
        mediaSession.setQueue(arrayList1);
        if (did > 0) {
            TLRPC.User user = users.get(did);
            if (user != null) {
                mediaSession.setQueueTitle(ContactsController.formatName(user.first_name, user.last_name));
            } else {
                mediaSession.setQueueTitle("DELETED USER");
            }
        } else {
            TLRPC.Chat chat = chats.get(-did);
            if (chat != null) {
                mediaSession.setQueueTitle(chat.title);
            } else {
                mediaSession.setQueueTitle("DELETED CHAT");
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    handlePlayRequest();
}
 
Example #21
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    ApplicationLoader.postInitApplication();

    lastSelectedDialog = MessagesController.getNotificationsSettings(currentAccount).getInt("auto_lastSelectedDialog", 0);

    mediaSession = new MediaSession(this, "MusicService");
    setSessionToken(mediaSession.getSessionToken());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, LaunchActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    extras.putBoolean(SLOT_RESERVATION_QUEUE, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_PREV, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_NEXT, true);
    mediaSession.setExtras(extras);

    updatePlaybackState(null);

    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidStarted);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidReset);
}
 
Example #22
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    String args[] = mediaId.split("_");
    if (args.length != 2) {
        return;
    }
    try {
        int did = Integer.parseInt(args[0]);
        int id = Integer.parseInt(args[1]);
        ArrayList<MessageObject> arrayList = musicObjects.get(did);
        ArrayList<MediaSession.QueueItem> arrayList1 = musicQueues.get(did);
        if (arrayList == null || id < 0 || id >= arrayList.size()) {
            return;
        }
        lastSelectedDialog = did;
        MessagesController.getNotificationsSettings(currentAccount).edit().putInt("auto_lastSelectedDialog", did).commit();
        MediaController.getInstance().setPlaylist(arrayList, arrayList.get(id), false);
        mediaSession.setQueue(arrayList1);
        if (did > 0) {
            TLRPC.User user = users.get(did);
            if (user != null) {
                mediaSession.setQueueTitle(ContactsController.formatName(user.first_name, user.last_name));
            } else {
                mediaSession.setQueueTitle("DELETED USER");
            }
        } else {
            TLRPC.Chat chat = chats.get(-did);
            if (chat != null) {
                mediaSession.setQueueTitle(chat.title);
            } else {
                mediaSession.setQueueTitle("DELETED CHAT");
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    handlePlayRequest();
}
 
Example #23
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    ApplicationLoader.postInitApplication();

    lastSelectedDialog = MessagesController.getNotificationsSettings(currentAccount).getInt("auto_lastSelectedDialog", 0);

    mediaSession = new MediaSession(this, "MusicService");
    setSessionToken(mediaSession.getSessionToken());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, LaunchActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    extras.putBoolean(SLOT_RESERVATION_QUEUE, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_PREV, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_NEXT, true);
    mediaSession.setExtras(extras);

    updatePlaybackState(null);

    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidStarted);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidReset);
}
 
Example #24
Source File: ClementineMediaSessionNotification.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setMediaSessionCompat(MediaSession mediaSession) {
    if (mediaSession == null) {
        return;
    }

    //mNotificationBuilder.setStyle(new Notification.MediaStyle()
    //        .setMediaSession(mediaSession.getSessionToken()));
}
 
Example #25
Source File: MediaSessionStack.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Notify the priority tracker that a session's playback state changed.
 *
 * @param record The record that changed.
 * @param oldState Its old playback state.
 * @param newState Its new playback state.
 */
public void onPlaystateChanged(MediaSessionRecord record, int oldState, int newState) {
    if (shouldUpdatePriority(oldState, newState)) {
        mSessions.remove(record);
        mSessions.add(0, record);
        clearCache(record.getUserId());
    } else if (!MediaSession.isActiveState(newState)) {
        // Just clear the volume cache when a state goes inactive
        mCachedVolumeDefault = null;
    }

    // In most cases, playback state isn't needed for finding media button session,
    // but we only use it as a hint if an app has multiple local media sessions.
    // In that case, we pick the media session whose PlaybackState matches
    // the audio playback configuration.
    if (mMediaButtonSession != null && mMediaButtonSession.getUid() == record.getUid()) {
        MediaSessionRecord newMediaButtonSession =
                findMediaButtonSession(mMediaButtonSession.getUid());
        if (newMediaButtonSession != mMediaButtonSession) {
            updateMediaButtonSession(newMediaButtonSession);
        }
    }
}
 
Example #26
Source File: MusicBrowserService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    ApplicationLoader.postInitApplication();

    lastSelectedDialog = MessagesController.getNotificationsSettings(currentAccount).getInt("auto_lastSelectedDialog", 0);

    mediaSession = new MediaSession(this, "MusicService");
    setSessionToken(mediaSession.getSessionToken());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, LaunchActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    extras.putBoolean(SLOT_RESERVATION_QUEUE, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_PREV, true);
    extras.putBoolean(SLOT_RESERVATION_SKIP_TO_NEXT, true);
    mediaSession.setExtras(extras);

    updatePlaybackState(null);

    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidStart);
    NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.messagePlayingDidReset);
}
 
Example #27
Source File: MediaSessionCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static Parcelable getSessionToken(Object sessionObj) {
    return ((MediaSession)sessionObj).getSessionToken();
}
 
Example #28
Source File: MediaSessionCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void setPlaybackState(Object sessionObj, Object stateObj) {
    ((MediaSession)sessionObj).setPlaybackState((PlaybackState)stateObj);
}
 
Example #29
Source File: MediaSessionCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void release(Object sessionObj) {
    ((MediaSession)sessionObj).release();
}
 
Example #30
Source File: MediaSessionCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void setMetadata(Object sessionObj, Object metadataObj) {
    ((MediaSession)sessionObj).setMetadata((MediaMetadata)metadataObj);
}