# VideoPlay **Repository Path**: yycodespace/video-play ## Basic Information - **Project Name**: VideoPlay - **Description**: 视频播放的主要工作是将视频数据转码并输出到设备进行播放,同时管理播放任务。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 17 - **Created**: 2025-08-06 - **Last Updated**: 2025-08-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Video Playback using AVPlayer ### Overview The main task of video playback is to transcode video data and output it to the device for playback, while managing playback tasks. This article will introduce and explain the development of scenarios such as the entire video playback process, silent playback, loop playback, and double speed playback. This example mainly demonstrates the functions related to playing local video, using interfaces such as @ ohos.multimedia.media, @ ohos.resomeManager, and @ ohoswidfiManager to achieve video playback, pause, adjust speed, mute playback, volume settings, window zoom mode settings, and other functions; The implementation effect is as follows. ### Preview | Play | Pause | Playback speed adjustment | |--------------------------------------------------|-------------------------------------------------|----------------------------------------------------| | ![Play.png](screenshots/devices/playing.png) | ![Pause.png](screenshots/devices/pause.png) | ![img_2.png](screenshots/devices/speed_dialog.png) | | Mute setting | Volume setting | Scale fit setting | | ![Mute.png](screenshots/devices/set_media_muted.png) | ![Volume.png](screenshots/devices/set_volume.png) | ![ScaleFit.png](screenshots/devices/scale_fit.png) | ### How to Use 1. Click on the video interface to bring up the video control panel, then click on the control panel again to disappear. If no action is taken, the control panel will automatically disappear within 5 seconds; 2. Click the pause/play button to control the video to pause playback; 3. Slide the video progress bar to jump to the designated location, and the time progress will appear in the middle of the video for users to easily view the video progress; 4. Click on the speed adjustment button to select 1.0, 1.25, 1.75, or 2.0 for speed adjustment; 5. Click the mute button to set the mute mode for playback; 6. Click the window zoom mode button to choose to stretch to the same size as the window or zoom to the shortest edge to fill the window; 7. Long press the screen to control the video to play at 2.0x speed; 8. Automatic loop playback of videos; 9. Click the exit arrow in the upper left corner to exit the application. ### Project Directory ``` ├──entry/src/main/ets // Code area │ ├──components │ │ ├──ExitVideo.ets // Exit the application component │ │ ├──ScaleDialog.ets // Window zoom mode setting dialog box │ │ ├──SetVolumn.ets // Set volume component │ │ ├──SpeedDialog.ets // Play double speed dialog box │ │ └──VideoOperate.ets // Video operation component │ ├──entryability │ │ └──EntryAbility.ets // Application entrance Ability │ ├──pages │ │ └──Index.ets // Home Video Interface │ └──utils │ ├──GlobalContext.ets // Common tools │ ├──ResourceUtil.ets // Common tools │ └──TimeUtils.ts // Video time helper class │────entry/src/main/resources // Static resources │ └────PlayerService ├──src/main/ets │ ├──common │ │ └──CommonConstants.ets // Common constant │ ├──component │ │ ├──VideoPlayer.ets // Public video playback component │ │ └──VideoToolBar.ets // Public Video Toolbar │ ├──controller │ │ ├──AvPlayerController.ets // Avplayer Public control category │ │ └──AvSessionController.ets // Session Public control category │ ├──model │ │ └──VideoData.ets // Video data category │ └──utils │ ├──BackgroundTaskManager.ets // Backend task related tools │ ├──ImageUtil.ets // Image Tool │ ├──Logger.ets // Log utility │ └──TimeUtils.ets // Video time helper class └──src/main/resources // General module resources ``` ### How to Implement + The functional interfaces for video speed adjustment, pause, play, video switching, and video navigation are encapsulated in AvPlayerController.ets. For source code reference, please see [AvPlayerController.ets](PlayerService/src/main/ets/controller/AvPlayerController.ets); + Use media.createAVPlayer() to obtain an AVPlayer object; + Speed switching: When selecting different speeds, call avPlayer.setSpeed(speed: PlaybackSpeed); + Pause and play: When clicking pause or play, call avPlayer.pause() or avPlayer.play(); + Video jump: Call avPlayer.seek() when dragging the slider + Mute playback: Call avPlayer.setMediaMuted() when the mute button is clicked + Volume setting: When sliding up or down with a gesture, the AVVolumePanel component is displayed and the volume value is calculated based on the sliding distance; + Window zoom mode setting: Set the videoScaleType property value of avPlayer when selecting different window zoom modes; + Long press for speed adjustment: When the gesture is long pressed, call avPlayer.setSpeed(speed: PlaybackSpeed); ### Required Permissions - ohos.permission.INTERNET - ohos.permission.GET_WIFI_INFO ### Dependencies N/A ### Constraints 1. The sample app is supported only on Huawei phones running the standard system. 2. The HarmonyOS version must be HarmonyOS 5.0.0 Release or later. 3. The DevEco Studio version must be DevEco Studio 5.0.0 Release or later. 4. The HarmonyOS SDK version must be HarmonyOS NEXT 5.0.0 Release SDK or later.