基于 View 的界面使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
添加依赖项
Kotlin
implementation("androidx.media3:media3-ui:1.8.0")
Groovy
implementation "androidx.media3:media3-ui:1.8.0"
PlayerView
最重要的组件是 PlayerView
,用于媒体播放的视图。PlayerView
在播放期间显示视频、图片、字幕和专辑封面,以及播放控件。
PlayerView
具有用于附加和分离(通过传递 null
)Player
实例的 setPlayer()
方法。
PlayerView
可用于视频、图片和音频播放。在播放视频时,它会呈现视频和字幕;在播放图片时,它会呈现位图;并且可以显示音频文件中包含的元数据中的封面图片。您可以像添加任何其他界面组件一样,将其添加到布局文件中。例如,可以使用以下 XML 包含 PlayerView
:
<androidx.media3.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:show_buffering="when_playing"
app:show_shuffle_button="true"/>
上面的代码段说明了 PlayerView
提供了多个属性。这些属性可用于自定义视图的行为以及外观和风格。这些属性中的大多数都有对应的 setter 方法,可用于在运行时自定义视图。PlayerView
文档中更详细地列出了这些属性和 setter 方法。
为了提供更舒适的用户体验,如果您使用的是 ExoPlayer,请考虑添加 keepScreenOn
Android 属性或设置唤醒锁定。您可以在后台工作页面中调查其他使设备保持唤醒状态的操作。
android:keepScreenOn="true"
在布局文件中声明视图后,可以在 activity 的 onCreate
方法中查找该视图:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
playerView = findViewById(R.id.player_view)
}
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
playerView = findViewById(R.id.player_view);
}
初始化播放器后,可以通过调用 setPlayer
将其附加到视图:
Kotlin
// Instantiate the player.
val player = ExoPlayer.Builder(context).build()
// Attach player to the view.
playerView.player = player
// Set the media item to be played.
player.setMediaItem(mediaItem)
// Prepare the player.
player.prepare()
Java
// Instantiate the player.
player = new ExoPlayer.Builder(context).build();
// Attach player to the view.
playerView.setPlayer(player);
// Set the media item to be played.
player.setMediaItem(mediaItem);
// Prepare the player.
player.prepare();
PlayerControlView
PlayerControlView
是 PlayerView
子视图之一,其中包含进度条和用于控制播放的按钮。请注意,PlayerControlView
不应作为独立组件在 PlayerView
之外使用。您可以通过在 PlayerView
上设置属性(这些属性将传递给 PlayerControlView
)或通过 android:id="@id/exo_controller
提供自定义控制器来自定义该功能。
选择表面类型
PlayerView
的 surface_type
属性可用于设置视频播放所用的界面类型。允许的值包括 surface_view
、texture_view
、spherical_gl_surface_view
(这是用于球形视频播放的特殊值)、video_decoder_gl_surface_view
(用于使用扩展渲染器的视频渲染)和 none
(仅用于音频播放)。如需详细了解应选择哪种 Surface 类型,请参阅 Surface 页面。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Getting started with Views-based UI\n\nAdd the dependency\n------------------\n\n### Kotlin\n\n```kotlin\nimplementation(\"androidx.media3:media3-ui:1.8.0\")\n```\n\n### Groovy\n\n```groovy\nimplementation \"androidx.media3:media3-ui:1.8.0\"\n```\n\nPlayerView\n----------\n\nThe most important component is [`PlayerView`](/reference/androidx/media3/ui/PlayerView), a view for media playback.\n`PlayerView` displays video, images, subtitles, and album art during playback,\nas well as playback controls.\n\n`PlayerView` has a [`setPlayer()`](/reference/androidx/media3/ui/PlayerView#setPlayer(androidx.media3.common.Player)) method for attaching and detaching (by\npassing `null`) [`Player`](/reference/androidx/media3/common/Player) instances.\n\n`PlayerView` can be used for both video, image and audio playbacks. It renders\nvideo and subtitles in the case of video playback, bitmaps for image playback\nand can display artwork included as metadata in audio files. You can include it\nin your layout files like any other UI component. For example, a `PlayerView`\ncan be included with the following XML: \n\n \u003candroidx.media3.ui.PlayerView\n android:id=\"@+id/player_view\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n app:show_buffering=\"when_playing\"\n app:show_shuffle_button=\"true\"/\u003e\n\nThe snippet above illustrates that `PlayerView` provides several attributes.\nThese attributes can be used to customize the view's behavior, as well as its\nlook and feel. Most of these attributes have corresponding setter methods, which\ncan be used to customize the view at runtime. The `PlayerView` documentation\nlists these attributes and setter methods in more detail.\n\nFor a more comfortable user experience, consider adding the `keepScreenOn` Android\nattribute or [setting a wake lock](/reference/androidx/media3/exoplayer/ExoPlayer#setWakeMode(int)), if you are using ExoPlayer. You can\ninvestigate other actions that keep the device awake in the [background work\npages](/develop/background-work/background-tasks/awake). \n\n android:keepScreenOn=\"true\"\n\nOnce the view is declared in the layout file, it can be looked up in the\n`onCreate` method of the activity: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n // ...\n playerView = findViewById(R.id.player_view)\n}\n```\n\n### Java\n\n```java\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n // ...\n playerView = findViewById(R.id.player_view);\n}\n```\n\n\u003cbr /\u003e\n\nWhen a player has been initialized, it can be attached to the view by calling\n`setPlayer`: \n\n### Kotlin\n\n```kotlin\n// Instantiate the player.\nval player = ExoPlayer.Builder(context).build()\n// Attach player to the view.\nplayerView.player = player\n// Set the media item to be played.\nplayer.setMediaItem(mediaItem)\n// Prepare the player.\nplayer.prepare()\n```\n\n### Java\n\n```java\n// Instantiate the player.\nplayer = new ExoPlayer.Builder(context).build();\n// Attach player to the view.\nplayerView.setPlayer(player);\n// Set the media item to be played.\nplayer.setMediaItem(mediaItem);\n// Prepare the player.\nplayer.prepare();\n```\n\n\u003cbr /\u003e\n\n### PlayerControlView\n\n[`PlayerControlView`](/reference/androidx/media3/ui/PlayerControlView) is one of `PlayerView` sub-Views that contains the\nprogress bar and buttons to control playback. Note that `PlayerControlView` is\nnot intended to be used a standalone component outside of `PlayerView`. It can\nbe customized by setting attributes on `PlayerView` (which will be passed onto\n`PlayerControlView`) or providing a custom controller with\n`android:id=\"@id/exo_controller`.\n\n### Choose a surface type\n\nThe `surface_type` attribute of `PlayerView` lets you set the type of surface\nused for video playback. The allowed values are `surface_view`, `texture_view`,\n`spherical_gl_surface_view` (which is a special value for spherical video\nplayback), `video_decoder_gl_surface_view` (which is for video rendering using\nextension renderers) and `none` (for audio playback only). More information on\nwhich surface type to pick can be found [on the Surface page](/media/media3/ui/surface)."]]