构建视频播放器 activity
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果 Activity 收到 onCreate()
生命周期回调方法,则应执行以下步骤:
- 创建并初始化媒体会话
- 设置媒体会话回调
- 将媒体会话的媒体按钮接收器设置为 Null,以使媒体按钮事件在播放器不可见时不会重新启动播放器。这只会影响 Android 5.0(API 级别 21)及更高版本的设备。
- 创建并初始化媒体控制器
以下 onCreate()
代码展示了这些步骤:
Kotlin
private lateinit var mediaSession: MediaSessionCompat
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Create a MediaSessionCompat
mediaSession = MediaSessionCompat(this, LOG_TAG).apply {
// Enable callbacks from MediaButtons and TransportControls
setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
// Do not let MediaButtons restart the player when the app is not visible
setMediaButtonReceiver(null)
// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
val stateBuilder = PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_PLAY_PAUSE)
setPlaybackState(stateBuilder.build())
// MySessionCallback has methods that handle callbacks from a media controller
setCallback(MySessionCallback())
}
// Create a MediaControllerCompat
MediaControllerCompat(this, mediaSession).also { mediaController ->
MediaControllerCompat.setMediaController(this, mediaController)
}
}
Java
MediaSessionCompat mediaSession;
PlaybackStateCompat.Builder stateBuilder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a MediaSessionCompat
mediaSession = new MediaSessionCompat(this, LOG_TAG);
// Enable callbacks from MediaButtons and TransportControls
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
// Do not let MediaButtons restart the player when the app is not visible
mediaSession.setMediaButtonReceiver(null);
// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
stateBuilder = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSession.setState(stateBuilder.build());
// MySessionCallback has methods that handle callbacks from a media controller
mediaSession.setCallback(new MySessionCallback());
// Create a MediaControllerCompat
MediaControllerCompat mediaController =
new MediaControllerCompat(this, mediaSession);
MediaControllerCompat.setMediaController(this, mediaController);
}
应用关闭后,Activity 会相继收到 onPause()
和 onStop()
回调。如果播放器处于播放状态,您必须在其 Activity 消失之前将其停止。具体选择使用哪一个回调,取决于您运行的 Android 版本。
在 Android 6.0(API 级别 23)及更早版本中,无法保证何时调用 onStop()
;可能会在您的 Activity 消失 5 秒后调用。因此,在 7.0 版以前的 Android 版本中,您的应用应在 onPause()
中停止播放。在 Android 7.0 及更高版本中,系统会在 Activity 变得不可见后立即调用 onStop()
,因此这不是问题。
总结:
- 在 Android 6.0 及更低版本中,在
onPause()
回调中停止播放器。
- 在 Android 7.0 及更高版本中,在
onStop()
回调中停止播放器。
当 Activity 收到 onDestroy()
回调后,应释放并清理您的播放器。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Building a video player activity\n\nWhen the activity receives the `onCreate()` lifecycle callback method it should perform these steps:\n\n- Create and [initialize the media session](/guide/topics/media-apps/working-with-a-media-session#init-session)\n- Set the media session callback\n- Set the media session's media button receiver to null so that a [media button event](/guide/topics/media-apps/mediabuttons#restarting-inactive-mediasessions) won't restart the player when it is not visible. This only affects Android 5.0 (API level 21) and higher devices.\n- Create and initialize the media controller\n\nThe `onCreate()` code below demonstrates these steps: \n\n### Kotlin\n\n```kotlin\nprivate lateinit var mediaSession: MediaSessionCompat\n\npublic override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n\n // Create a MediaSessionCompat\n mediaSession = MediaSessionCompat(this, LOG_TAG).apply {\n\n // Enable callbacks from MediaButtons and TransportControls\n setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or\n MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)\n\n // Do not let MediaButtons restart the player when the app is not visible\n setMediaButtonReceiver(null)\n\n // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player\n val stateBuilder = PlaybackStateCompat.Builder()\n .setActions(PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_PLAY_PAUSE)\n setPlaybackState(stateBuilder.build())\n\n // MySessionCallback has methods that handle callbacks from a media controller\n setCallback(MySessionCallback())\n }\n\n // Create a MediaControllerCompat\n MediaControllerCompat(this, mediaSession).also { mediaController -\u003e\n MediaControllerCompat.setMediaController(this, mediaController)\n }\n}\n```\n\n### Java\n\n```java\nMediaSessionCompat mediaSession;\nPlaybackStateCompat.Builder stateBuilder;\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n\n // Create a MediaSessionCompat\n mediaSession = new MediaSessionCompat(this, LOG_TAG);\n\n // Enable callbacks from MediaButtons and TransportControls\n mediaSession.setFlags(\n MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |\n MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);\n\n // Do not let MediaButtons restart the player when the app is not visible\n mediaSession.setMediaButtonReceiver(null);\n\n // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player\n stateBuilder = new PlaybackStateCompat.Builder()\n .setActions(\n PlaybackStateCompat.ACTION_PLAY |\n PlaybackStateCompat.ACTION_PLAY_PAUSE);\n mediaSession.setState(stateBuilder.build());\n\n // MySessionCallback has methods that handle callbacks from a media controller\n mediaSession.setCallback(new MySessionCallback());\n\n // Create a MediaControllerCompat\n MediaControllerCompat mediaController =\n new MediaControllerCompat(this, mediaSession);\n\n MediaControllerCompat.setMediaController(this, mediaController);\n}\n```\n\nWhen an app is closed, the activity receives the `onPause()` and `onStop()` callbacks in succession. If the player is playing, you must stop it before its activity goes away. The choice of which callback to use depends on what Android version you're running.\n\nIn Android 6.0 (API level 23) and earlier there is no guarantee of when `onStop()` is called; it could get called 5 seconds after your activity disappears. Therefore, in Android versions earlier than 7.0, your app should stop playback in `onPause()`. In Android 7.0 and beyond, the system calls `onStop()` as soon as the activity becomes not visible, so this is not a problem.\n\nTo summarize:\n\n- In Android version 6.0 and earlier, stop the player in the `onPause()` callback.\n- In Android version 7.0 and later, stop the player in the `onStop()` callback.\n\nWhen the activity receives the `onDestroy()` callback, it should release and clean up your player."]]