HarmonyOS provides comprehensive multimedia capabilities for capturing and reproducing audio content through the AudioRecorder and AudioPlayer modules. This implementation demonstrates a complete voice recording application that handles audio capture, file persistence, media library management, and playback controls.
Core Concepts
The AudioRecorder API manages the audio capture pipeline, handling microphone input, encoding configuration, and file serialization. Developers can specify sampling rates, channel configurations, encoding formats, and output containers. The AudioPlayer API handles audio decoding, digital-to-analog conversion, and playback state management through the system's audio subsystem.
Functional Overview
The application implements a three-tier interaction model:
Audio Capture Interface The recording view provides real-time visualization of audio input levels during capture. Users initiate rceording through a primary action button, with secondary controls for pausing and resuming the capture session. Terminating the recording persists the encoded audio to local storage and returns to the file browser.
Media Library Management The primary view presents a scrollable list of recorded audio files stored in the application's media directory. Individual items support tap-to-play navigation and swipe gestures revealing contextual actions. Batch operations enable multi-selection for bulk deletion or metadata modification.
Playback Controls The audio player view manages the rendering lifecycle, supporting play, pause, and resume operations with position tracking. The interface automatically handles audio focus changes and playback completion events.
Project Architecture
entry/src/main/ets/
|---components
| |---WaveformVisualizer.ets // Audio amplitude animation
| |---TrackListItem.ets // Individual recording entry
| |---Chronometer.ets // Recording duration timer
| |---SelectionHeader.ets // Multi-select state indicator
| |---BrowserView.ets // Main file browser
| |---MediaController.ets // Playback control surface
| |---MetadataEditor.ets // Rename dialog implementation
| |---NavigationBar.ets // Primary toolbar
| |---BackNavigation.ets // Detail view header
|---ability
| |---EntryAbility.ets
|---service
| |---AudioCaptureService.ts // Recording orchestration
| |---TimestampUtil.ts // Date formatting utilities
| |---DiagnosticLog.ts // Logging infrastructure
| |---MediaRepository.ts // File system abstraction
| |---SessionTimer.ts // Elapsed time tracking
| |---FileMetadata.ts // Recording entity model
| |---SystemBridge.ts // Platform utilities
|---views
| |---Library.ets // File browser page
| |---Player.ets // Playback interface
| |---Recorder.ets // Capture interface
Implementation Strategy
Audio Capture Module
The recording service initializes an AudioRecorder instance with AAC encoding and MP4 container format. Configuration parameters include 44.1kHz sampling rate and mono channel layout. The service maintains a RecordingState enumeration to manage transitions between idle, capturing, and paused states. File output utilizes the media library's FileAsset API to generate timestamped filenames in the application's private storage.
Media Management Module
The repository layer queries the media library through getMediaLibrary() to enumerate audio files with specific MIME type filters. For metadata modification, the system presents a modal dialog invoking rename() on the selected FileAsset. Deletion operations require user confirmation before calling delete() and refreshing the observable data source.
Playback Module
The player service instantiates an AudioPlayer bound to the selected file URI. Lifecycle hooks manage resource allocation: onPageShow() initializes the audio sink and begins buffering, while onPageHide() releases the player instance to conserve system resources. State observers update the UI to reflect buffering, playing, and completed states.
Required Permissions
The application requires explicit user grants for the following capabilities:
ohos.permission.MICROPHONE- Access to device microphones for audio captureohos.permission.WRITE_MEDIA- Persistence of encoded audio files to shared storageohos.permission.READ_MEDIA- Retrieval of media metadata and file content
System Constraints
This implementation targets standard-system devices running API version 9 (SDK 3.2.14.5). Development requires DevEco Studio 3.1.1 Release or later. The application utilizes system-level interfaces requiring Full SDK configuration with @ohos.process permissions. Compilation requires manual integration of system image libraries when using the Full SDK variant.