1 page.title= 2 page.tags=tv, mediasession 3 4 trainingnavtop=true 5 6 @jd:body 7 8 <div id="tb-wrapper"> 9 <div id="tb"> 10 <h2></h2> 11 <ol> 12 <li><a href="#session">Media Session </a></li> 13 <li><a href="#card"></a></li> 14 <li><a href="#state"></a></li> 15 <li><a href="#respond"> </a></li> 16 </ol> 17 18 </div> 19 </div> 20 21 <p>TV Android [<em></em>] TV </p> 22 23 <p>[] </p> 24 25 <p>{@link android.media.session.MediaSession} [] </p> 26 27 <h2 id="session">Media Session </h2> 28 29 <p><a href="{@docRoot}guide/components/activities"></a><a href="{@docRoot}guide/components/services"></a> {@link android.service.media.MediaBrowserService} </p> 30 31 <p>{@link android.service.media.MediaBrowserService#onCreate() onCreate()}{@link android.media.session.MediaSession#MediaSession(android.content.Context, java.lang.String) MediaSession} {@link android.service.media.MediaBrowserService} </p> 32 33 <pre> 34 mSession = new MediaSession(this, "MusicService"); 35 mSession.setCallback(new MediaSessionCallback()); 36 mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | 37 MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); 38 39 // for the MediaBrowserService 40 setSessionToken(mSession.getSessionToken()); 41 </pre> 42 43 <p class="note"><strong>:</strong>[] {@link android.media.session.MediaSession#FLAG_HANDLES_TRANSPORT_CONTROLS} </p> 44 45 <h2 id="card"></h2> 46 47 <p>[] {@link android.media.session.MediaSession#setActive(boolean) setActive(true)} <a href="{@docRoot}training/managing-audio/audio-focus"> </a> </p> 48 49 <pre> 50 private void handlePlayRequest() { 51 52 tryToGetAudioFocus(); 53 54 if (!mSession.isActive()) { 55 mSession.setActive(true); 56 } 57 ... 58 </pre> 59 60 <p>{@link android.media.session.MediaSession#setActive(boolean) setActive(false)} 530 i</p> 61 62 <h2 id="state"></h2> 63 64 <p> {@link android.media.session.MediaSession} </p> 65 66 <pre> 67 private void updatePlaybackState() { 68 long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN; 69 if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { 70 position = mMediaPlayer.getCurrentPosition(); 71 } 72 PlaybackState.Builder stateBuilder = new PlaybackState.Builder() 73 .setActions(getAvailableActions()); 74 stateBuilder.setState(mState, position, 1.0f); 75 mSession.setPlaybackState(stateBuilder.build()); 76 } 77 private long getAvailableActions() { 78 long actions = PlaybackState.ACTION_PLAY | 79 PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | 80 PlaybackState.ACTION_PLAY_FROM_SEARCH; 81 if (mPlayingQueue == null || mPlayingQueue.isEmpty()) { 82 return actions; 83 } 84 if (mState == PlaybackState.STATE_PLAYING) { 85 actions |= PlaybackState.ACTION_PAUSE; 86 } 87 if (mCurrentIndexOnQueue > 0) { 88 actions |= PlaybackState.ACTION_SKIP_TO_PREVIOUS; 89 } 90 if (mCurrentIndexOnQueue < mPlayingQueue.size() - 1) { 91 actions |= PlaybackState.ACTION_SKIP_TO_NEXT; 92 } 93 return actions; 94 } 95 </pre> 96 97 <h2 id="metadata"></h2> 98 99 <p>{@link android.media.session.MediaSession#setMetadata(android.media.MediaMetadata) setMetadata()} {@link android.media.MediaMetadata} [] {@code MediaData} </p> 100 101 <pre> 102 private void updateMetadata(MediaData myData) { 103 MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder(); 104 // To provide most control over how an item is displayed set the 105 // display fields in the metadata 106 metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, 107 myData.displayTitle); 108 metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, 109 myData.displaySubtitle); 110 metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, 111 myData.artUri); 112 // And at minimum the title and artist for legacy support 113 metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, 114 myData.title); 115 metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, 116 myData.artist); 117 // A small bitmap for the artwork is also recommended 118 metadataBuilder.putString(MediaMetadata.METADATA_KEY_ART, 119 myData.artBitmap); 120 // Add any other fields you have for your data as well 121 mSession.setMetadata(metadataBuilder.build()); 122 } 123 </pre> 124 125 <h2 id="respond"> </h2> 126 127 <p> [] TV {@link android.app.PendingIntent} {@link android.media.session.MediaSession#setSessionActivity(android.app.PendingIntent) setSessionActivity()} </p> 128 129 <pre> 130 Intent intent = new Intent(mContext, MyActivity.class); 131 PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, 132 intent, PendingIntent.FLAG_UPDATE_CURRENT); 133 mSession.setSessionActivity(pi); 134 </pre> 135