# AVPlayerBasicControl **Repository Path**: harmonyos_samples/avplayer-basic-control ## Basic Information - **Project Name**: AVPlayerBasicControl - **Description**: AVPlayer自有基础播控能力,主要包括播放、暂停、跳转播放、静音播放、循环播放、窗口缩放模式设置、倍速设置、音量设置等场景。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2025-08-25 - **Last Updated**: 2026-01-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Basic Video Playback Using AVPlayer ## Overview This sample demonstrates how to use AVPlayer to implement local video playback, including video loading, play/pause, exit, seeking, mute control, loop playback, window scaling mode setting, playback speed adjustment, volume adjustment, and subtitle loading. ## Effect | Play | Pause | Playback Speed Adjustment | |-----------------------------------------------------------|---------------------------------------------------------|------------------------------------------------------------------| | | | | | Mute Control | Volume Adjustment | Window Scaling Mode Setting | |------------------------------------------------------|-------------------------------------------------|--------------------------------------------------------------| | | | | ## How to Use 1. Download, compile, and run the sample. 2. Once the running is successful, the video starts playing automatically. 3. Tap the video screen to show the video control panel; tap again to hide it. If no action is taken for 5 seconds, the panel hides automatically. 4. Use the pause/play button to toggle the video's play state. 5. Drag the video progress bar to seek to a specific position. The playback time appears in the center of the video to indicate progress. 6. Tap to adjust playback speed, with options for 1.0x, 1.25x, 1.75x, or 2.0x. 7. Tap the mute button to mute the video. 8. Tap the window scaling button to either stretch the video to match the window size or scale it so its shortest edge fills the window. 9. Press and hold the screen to play the video at 2.0x speed. 10. Swipe up or down on the screen to adjust the volume. 11. Subtitles are displayed below the video. You can tap the language switch button to change the subtitle language. 12. The video plays in a continuous loop automatically. 13. Tap the exit arrow in the upper-left corner to close the application. ## Project Directory ``` ├──entry/src/main/ets // Code area │ ├──common │ │ ├──constants │ │ │ └──CommonConstants.ets // Common constants │ │ └──utils │ │ ├──GlobalContext.ets // Common utilities │ │ └──TimeUtils.ts // Video time helper class │ ├──views │ │ ├──languageDialog.ets // Dialog for switching bullet comment languages │ │ ├──ScaleDialog.ets // Dialog for setting the window scaling mode │ │ ├──SetVolumn.ets // Component for adjusting the volume │ │ ├──SpeedDialog.ets // Dialog for adjusting the playback speed │ │ └──VideoOperate.ets // Component for operating videos │ ├──controller │ │ └──AvPlayerController.ets // AVPlayer control class │ ├──entryability │ │ └──EntryAbility.ets // Application entry ability │ ├──model │ │ └──VideoData.ets // Video data class │ └──pages │ └──Index.ets // Home page └──entry/src/main/resources // Application resources ``` ## How to Implement + Use the APIs encapsulated in AvPlayerController.ets to implement video speed adjustment, pause, play, video switching, and seeking,Source code reference: [AvPlayerController.ets](entry/src/main/ets/controller/AvPlayerController.ets); + Use media.createAVPlayer() to obtain an AVPlayer object. + Playback speed adjustment: call avPlayer.setSpeed(speed: PlaybackSpeed). + Play/Pause: call avPlayer.play() or avPlayer.pause() when the corresponding button is tapped. + Video seeking: call avPlayer.seek() while dragging the slider. + Mute control: call avPlayer.setMediaMuted() when the mute button is tapped. + Volume adjustment: add a PanGesture (up/down swipe) to the element. Display the AVVolumePanel component during swiping, and calculate the volume based on the swipe distance. + Window scaling mode: set the videoScaleType property of AVPlayer when selecting different scaling modes. + Long-press speed change: add a LongPressGesture listener to the element, and call avPlayer.setSpeed(speed: PlaybackSpeed) when the element is long-pressed. + Loop playback: set the loop property of AVPlayer to true when the video is in the prepared state. + Subtitle loading: call avPlayer.addSubtitleFromFd() during video initialization to set external subtitle resources. ## Required Permissions N/A ## Constraints 1.This sample is only supported on Huawei phones running standard systems. 2.The HarmonyOS version must be HarmonyOS 5.1.0 Release or later. 3.The DevEco Studio version must be DevEco Studio 5.1.0 Release or later. 4.The HarmonyOS SDK version must be HarmonyOS 5.1.0 Release SDK or later.