Android TV -2.4- Displaying a Now Playing Card
2016-02-10 09:02
441 查看
Displaying a Now Playing Card
PreviousNext
This lesson teaches you to
Start a Media SessionDisplay a Now Playing Card
Update the Playback State
Respond to User Action
TV apps must display a Now Playing card when playing media behind the launcher or in the background. This card allows users to return to the app that is currently playing media.
The Android framework displays a Now Playing card on the home screen when there is an active
MediaSession.
The card includes media metadata such as album art, title, and app icon. When the user selects the card, the system opens the app.
This lesson shows how to use the
MediaSessionclass
to implement the Now Playing card.
Figure 1. Display a Now Playing card when playing media in the background.
Start a Media Session
Create a MediaSessionwhen
your app is preparing to play media. The following code snippet is an example of how to set the appropriate callback and flags:
mSession = new MediaSession(this, "MusicService"); mSession.setCallback(new MediaSessionCallback()); mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
<p class="note"
Note: The Now Playing card will display only for a media session with the
FLAG_HANDLES_TRANSPORT_CONTROLSflag
set.
Display a Now Playing Card
The Now Playing card only appears for active sessions. You must call setActive(true)when
playback begins. Your app must also request audio focus, as described in Managing Audio Focus.
private void handlePlayRequest() { tryToGetAudioFocus(); if (!mSession.isActive()) { mSession.setActive(true); } ...
The card is removed from the launcher screen when a
setActive(false)call
deactivates the media session, or when another app initiates media playback. If playback is completely stopped and there is no active media, your app should deactivate the media session immediately. If playback is paused, your app should deactivate the media
session after a delay, usually between 5 to 30 minutes.
Update the Playback State
Update the playback state in the MediaSessionso
the card can show the state of the current media.
private void updatePlaybackState() { long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN; if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { position = mMediaPlayer.getCurrentPosition(); } PlaybackState.Builder stateBuilder = new PlaybackState.Builder() .setActions(getAvailableActions()); stateBuilder.setState(mState, position, 1.0f); mSession.setPlaybackState(stateBuilder.build()); } private long getAvailableActions() { long actions = PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PLAY_FROM_SEARCH; if (mPlayingQueue == null || mPlayingQueue.isEmpty()) { return actions; } if (mState == PlaybackState.STATE_PLAYING) { actions |= PlaybackState.ACTION_PAUSE; } else { actions |= PlaybackState.ACTION_PLAY; } if (mCurrentIndexOnQueue > 0) { actions |= PlaybackState.ACTION_SKIP_TO_PREVIOUS; } if (mCurrentIndexOnQueue < mPlayingQueue.size() - 1) { actions |= PlaybackState.ACTION_SKIP_TO_NEXT; } return actions; }
Display the Media Metadata
Set the MediaMetadatawith the
setMetadata()method.
This method of the media session object lets you provide information to the Now Playing card about the track such as the title, subtitle, and various icons. The following example assumes your track's data is stored in a custom data class,
MediaData.
private void updateMetadata(MediaData myData) { MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder(); // To provide most control over how an item is displayed set the // display fields in the metadata metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE, myData.displayTitle); metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, myData.displaySubtitle); metadataBuilder.putString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, myData.artUri); // And at minimum the title and artist for legacy support metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, myData.title); metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, myData.artist); // A small bitmap for the artwork is also recommended metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, myData.artBitmap); // Add any other fields you have for your data as well mSession.setMetadata(metadataBuilder.build()); }
Respond to User Action
When the user selects the Now Playing card, the system opens the app that owns the session. If your app provides a PendingIntentto
setSessionActivity(),
the system launches the activity you specify, as demonstrated below. If not, the default system intent opens. The activity you specify must provide playback controls that allow users to pause or stop playback.
Intent intent = new Intent(mContext, MyActivity.class); PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent, PendingIntent.FLAG_UPDATE_CURRENT); mSession.setSessionActivity(pi);
Next:
Enabling Background Playback
Get news & tips
相关文章推荐
- Android TV -2.2- Providing a Card View
- Android TV -2.1- Creating a Catalog Browser
- Android TV -1.4 Creating TV Navigation
- Android TV - 1.2 Handling TV Hardware
- 【Android】ColorPickerDialog 自定义颜色选择器的实现
- Android的minSdkVersion,targetSdkVersion,maxSdkVersion
- 实用的adb 命令连接 android 电视
- #Android源码#MeasureSpec
- 积跬步至千里系列之八--Android系统设置(一)
- android 双击退出
- Android下SDL2实现五子棋游戏
- 不允许错过的Anndroid技术经验60条
- Android Material Design theme
- Android中Textview超链接实现方式
- 乐视tv真机开发调试,乐视盒子开发者模式,乐视android 开发调试模式
- android对SQLite数据库进行操作(一)SQL语句
- android Button 切换背景,实现动态按钮和按钮颜色渐变
- Android 动画 6问6答
- Android View事件机制 21问21答
- Android Scroll 学习笔记