基于AVPlayer播放嵌入式短视频,主要包括视频列表切换(进度保持)、列表中第一个视频自动播放、基本播控、焦点管理、视频首帧显示(用来显示列表中每个视频)等场景。
Access rate limit exceeded. Please log in to continue.
Embedded Short Video Playback Using AVPlayer
Overview
This sample demonstrates how to develop embedded short video playback applications withAVPlayerandXComponent,You can useLazyForEach()to perform lazy data loading, enabling automatic video switching as the list scrolls and allowing continuous playback when entering the details page.
Effect

How to Use
-
Swipe down to play the first visible video.
-
Tap a video to navigate to its details page, where playback will continue seamlessly.
Project Directory
├──entry/src/main/ets
│ ├──common
│ │ ├──constants
│ │ │ └──Constants.ets // Common constants
│ │ └──utils
│ │ ├──AppRouter.ets // Page redirection method definition
│ │ ├──GlobalContext.ets // Common utilities
│ │ ├──TimeUtils.ets // Time utility
│ │ └──WindowUtil.ets // Window utility
│ ├──component
│ │ ├──AVVolumePanelView.ets // Volume Control Component
│ │ ├──BulletCommentView.ets // Danmu Display Component
│ │ ├──CaptionFontView.ets // Subtitle font setting component
│ │ ├──LanguageDialog.ets // Dialog for switching bullet comment languages
│ │ ├──ScaleDialog.ets // Dialog for setting the window scaling mode
│ │ ├──ScreenBrightnessView.ets // Brightness control component
│ │ ├──SpeedDialog.ets // Dialog for adjusting the playback speed
│ │ ├──VideoDetail.ets // Components and methods for seamless playback transition
│ │ └──VideoList.ets // Components and methods for the video list
│ ├──controller
│ │ ├──AvPlayerController.ets // AVPlayer control class
│ │ └──PiPWindowController.ets // Picture in Picture Public Control Class
│ ├──entryability
│ │ └──EntryAbility.ets // EntryAbility lifecycle callbacks
│ ├──entrybackupability
│ │ └──EntryBackupAbility.ets // EntryBackupAbility lifecycle callbacks
│ ├──model
│ │ ├──BulletComment.ets // Bullet comment data class
│ │ ├──CaptionFont.ets // Subtitle data type
│ │ ├──VideoData.ets // Video data class
│ │ ├──VideoDataSource.ets // Operations on the video list data source
│ │ ├──VideoInfo.ets // Video type definition
│ │ ├──VideoItemData.ets // Simulated data
│ │ └──VideoParams.ets // Page parameter type definition
│ └──pages
│ ├──DetailPage.ets // Video details page
│ └──PlayVideo.ets // Video list page
├──src/main/resources
│ └──base/profile
│ └──route_map.json // Page route configuration
└──module.json5
How to Implement
- Use LazyForEach to lazy load the video list and use AVPlayer on the XComponent to play videos.
- Use the fetchFrameByTime() method to obtain the thumbnail of the video based on the video playback link and display the thumbnail on the page.
- Listen for the video list scrolling stop event, calculate the index of the first visible video based on the scrolling offset, and play the video.
- When a user taps a video, the current playback status and surface ID are stored, and the playback continues on the video details page.
- When a user taps the return button, the video continues to be played based on the saved surface ID.
Required Permissions
N/A
Constraints
- This sample is only supported on Huawei phones running standard systems.
- The HarmonyOS version must be HarmonyOS 5.1.1 Release or later.
- The DevEco Studio version must be DevEco Studio 5.1.1 Release or later.
- The HarmonyOS SDK version must be HarmonyOS 5.1.1 Release SDK or later.