FileDocCategorySizeDatePackage
PlayerSession.javaAPI DocAndroid 5.1 API9085Thu Mar 12 22:22:44 GMT 2015com.android.onemedia

PlayerSession

public class PlayerSession extends Object

Fields Summary
private static final String
TAG
protected android.media.session.MediaSession
mSession
protected android.content.Context
mContext
protected com.android.onemedia.playback.Renderer
mRenderer
protected MediaSession.Callback
mCallback
protected Renderer.Listener
mRenderListener
protected MediaMetadata.Builder
mMetadataBuilder
protected ArrayList
mQueue
protected boolean
mUseQueue
protected android.media.session.PlaybackState
mPlaybackState
protected Listener
mListener
private String
mContent
Constructors Summary
public PlayerSession(android.content.Context context)


       
        mContext = context;
        mRenderer = new LocalRenderer(context, null);
        mCallback = new SessionCb();
        mRenderListener = new RenderListener();
        PlaybackState.Builder psBob = new PlaybackState.Builder();
        psBob.setActions(PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY);
        mPlaybackState = psBob.build();
        mQueue = new ArrayList<MediaSession.QueueItem>();

        mRenderer.registerListener(mRenderListener);

        initMetadata();
    
Methods Summary
public voidcreateSession()

        releaseSession();

        MediaSessionManager man = (MediaSessionManager) mContext
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
        Log.d(TAG, "Creating session for package " + mContext.getBasePackageName());

        mSession = new MediaSession(mContext, "OneMedia");
        mSession.setCallback(mCallback);
        mSession.setPlaybackState(mPlaybackState);
        mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
                | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
        mSession.setActive(true);
        updateMetadata();
    
public MediaSession.TokengetSessionToken()

        return mSession.getSessionToken();
    
private voidinitMetadata()

        mMetadataBuilder = new MediaMetadata.Builder();
        mMetadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE,
                "OneMedia display title");
        mMetadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
                "OneMedia display subtitle");

        mQueue.add(new QueueItem(mMetadataBuilder.build().getDescription(), 11));
    
public voidonDestroy()

        releaseSession();
        if (mRenderer != null) {
            mRenderer.unregisterListener(mRenderListener);
            mRenderer.onDestroy();
        }
    
private voidreleaseSession()

        if (mSession != null) {
            mSession.release();
            mSession = null;
        }
    
public voidsetContent(android.os.Bundle request)

        mRenderer.setContent(request);
        mContent = request.getString(RequestUtils.EXTRA_KEY_SOURCE);
    
public voidsetIcon(android.graphics.Bitmap icon)

        mMetadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON, icon);
        mQueue.clear();
        mQueue.add(new QueueItem(mMetadataBuilder.build().getDescription(), 11));
        updateMetadata();
    
public voidsetListener(com.android.onemedia.PlayerSession$Listener listener)

        mListener = listener;
    
public voidsetNextContent(android.os.Bundle request)

        mRenderer.setNextContent(request);
    
private voidupdateMetadata()

        // This is a mild abuse of metadata and shouldn't be duplicated in real
        // code
        if (mSession != null && mSession.isActive()) {
            mSession.setMetadata(mMetadataBuilder.build());
            // Just toggle the queue every time we update for testing
            mSession.setQueue(mUseQueue ? mQueue : null);
            mSession.setQueueTitle(mUseQueue ? "Queue title" : null);
            mUseQueue = !mUseQueue;
        }
    
private voidupdateState(int newState)

        float rate = newState == PlaybackState.STATE_PLAYING ? 1 : 0;
        long position = mRenderer == null ? -1 : mRenderer.getSeekPosition();
        PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState);
        bob.setState(newState, position, rate, SystemClock.elapsedRealtime());
        bob.setErrorMessage(null);
        mPlaybackState = bob.build();
        mSession.setPlaybackState(mPlaybackState);