HarmonyOS AVPlayer Long Video Development: Smooth Interaction, Stable Playback & PiP
This article details three core aspects of building cinema-grade long video apps with HarmonyOS AVPlayer: smooth gesture-based brightness and orientation controls, stable playback via audio focus management and seamless foreground/background resume, and full functionality including picture-in-picture and background audio playback.
Interaction Smoothness: Gesture Controls and Orientation Switching
Long video viewing involves frequent brightness adjustments and orientation changes. The article demonstrates two key interaction patterns:
1. Invisible Gesture Brightness Control
Instead of a traditional progress bar, a transparent slider at the screen edge combined with setWindowBrightness() enables real-time brightness adjustment via vertical finger swipes, significantly improving tactile responsiveness.
2. Flexible Landscape/Portrait Switching
AVPlayer provides flexible screen orientation control:
Auto-sensing: Set orientation to auto_rotation_restricted for sensor-based automatic switching.
Manual lock: Use window.Orientation API to respond to user-initiated switches, accommodating personalized viewing postures.
Animated GIFs illustrate both interactions.
Playback Stability: Handling Interruptions
Incoming calls and background switches are mandatory scenarios for long video playback. Proper handling ensures continuity.
1. Audio Focus Management
When subsequent audio (e.g., calls, alarms) occurs, the system forcibly interrupts the playing app. By listening to the audioInterrupt event, the app should automatically pause on interruption start and optionally resume after interruption ends, avoiding audio conflicts and abrupt experience.
2. Seamless Foreground/Background Resume
When users temporarily leave and return, video should continue from the exact progress, not restart.
Page lifecycle: Pause in onPageHide, resume in onPageShow.
Precise progress control: Combine AVPlayer's seek capability to accurately locate the second the user left, achieving smooth "resume from breakpoint".
A GIF demonstrates the seamless resume.
Full Functionality: Picture-in-Picture and Background Audio
Multitasking is mainstream; long video apps need "small window playback" and "background audio".
1. Picture-in-Picture (PiP)
Supporting "watching while messaging" is a user demand. Enable PiP via setAutoStartEnabled(true) and register stateChange and controlPanelActionEvent callbacks to manage PiP lifecycle and user play/pause actions, keeping video floating at hand.
2. Background Audio Playback
For audiobook and radio scenarios, audio must continue when screen locked or app backgrounded. Declare audioPlayback background task permission in module.json5 and manage via backgroundTaskManager to ensure continuous audio output.
GIFs illustrate both features.
Extended Reading: Comprehensive Playback Guide
The AVPlayer Video Playback Series Practices document covers additional high-frequency scenarios:
Short video: Millisecond-level start-on-swipe via LazyForEach loading and instance reuse.
Network video: Weak network anti-jitter, HLS/DASH compatibility, smart buffering strategies.
Embedded short video: Best practices for in-feed playback.
Basic playback control: Standard implementation of play/pause, speed, mute.
Links to official HarmonyOS developer documentation are provided for each scenario.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
HarmonyOS Developer Technology
HarmonyOS developers provide key technology analysis, version updates, Codelabs practice, and event information for HarmonyOS. Welcome developers to join the HarmonyOS ecosystem and create infinite possibilities together!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
