Search in sources :

Example 1 with RemoteUserInfo

use of androidx.media.MediaSessionManager.RemoteUserInfo in project media by androidx.

the class MediaSessionServiceLegacyStub method onGetRoot.

@Override
@Nullable
public BrowserRoot onGetRoot(String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
    RemoteUserInfo info = getCurrentBrowserInfo();
    MediaSession.ControllerInfo controller = createControllerInfo(info);
    AtomicReference<MediaSession.ConnectionResult> resultReference = new AtomicReference<>();
    ConditionVariable haveResult = new ConditionVariable();
    postOrRun(sessionImpl.getApplicationHandler(), () -> {
        resultReference.set(sessionImpl.onConnectOnHandler(controller));
        haveResult.open();
    });
    try {
        haveResult.block();
    } catch (InterruptedException e) {
        Log.e(TAG, "Couldn't get a result from onConnect", e);
        return null;
    }
    MediaSession.ConnectionResult result = resultReference.get();
    if (!result.isAccepted) {
        return null;
    }
    connectedControllersManager.addController(info, controller, result.availableSessionCommands, result.availablePlayerCommands);
    // No library root, but keep browser compat connected to allow getting session.
    return MediaUtils.defaultBrowserRoot;
}
Also used : ConditionVariable(androidx.media3.common.util.ConditionVariable) AtomicReference(java.util.concurrent.atomic.AtomicReference) RemoteUserInfo(androidx.media.MediaSessionManager.RemoteUserInfo) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) Nullable(androidx.annotation.Nullable)

Example 2 with RemoteUserInfo

use of androidx.media.MediaSessionManager.RemoteUserInfo in project media by androidx.

the class MediaSessionLegacyStub method onMediaButtonEvent.

@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
    @Nullable KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (keyEvent == null || keyEvent.getAction() != KeyEvent.ACTION_DOWN) {
        return false;
    }
    RemoteUserInfo remoteUserInfo = sessionCompat.getCurrentControllerInfo();
    int keyCode = keyEvent.getKeyCode();
    switch(keyCode) {
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
            if (keyEvent.getRepeatCount() == 0) {
                if (mediaPlayPauseKeyHandler.hasPendingMediaPlayPauseKey()) {
                    mediaPlayPauseKeyHandler.clearPendingMediaPlayPauseKey();
                    onSkipToNext();
                } else {
                    mediaPlayPauseKeyHandler.addPendingMediaPlayPauseKey(remoteUserInfo);
                }
            } else {
                // Consider long-press as a single tap. Handle immediately.
                handleMediaPlayPauseOnHandler(remoteUserInfo);
            }
            return true;
        default:
            // pending play/pause as a single tap to handle media keys in order.
            if (mediaPlayPauseKeyHandler.hasPendingMediaPlayPauseKey()) {
                handleMediaPlayPauseOnHandler(remoteUserInfo);
            }
            break;
    }
    return false;
}
Also used : KeyEvent(android.view.KeyEvent) RemoteUserInfo(androidx.media.MediaSessionManager.RemoteUserInfo) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)2 RemoteUserInfo (androidx.media.MediaSessionManager.RemoteUserInfo)2 KeyEvent (android.view.KeyEvent)1 ConditionVariable (androidx.media3.common.util.ConditionVariable)1 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1