Audio Player Manager
Audio Player Manager
Importing Modules
import audiokit from '@system.audiokit'
API Definitions
getPlayers
Queries the list of available AudioPlayer objects in the system.
getActivePlayer
Queries the currently active AudioPlayer object in the system.
subscribe
Listens for changes to audio players in the system. The PlayerEvent parameter of the callback is a notification event. The ID returned by this method can be used with the unsubscribe() method to stop listening.
Type signature of PlayerEvent:
type PlayerEvent = {
notify: string; // Change event type
player: string; // Name of the changed player
}
Change event types
active: The currently active player in the system has changedappend: A player has been added to the systemremove: A player has been removed from the system
unsubscribe
Cancels the player change listener. subscribeID is the ID value returned by the subscribe() method.
AudioPlayer Object
Type Signature
interface AudioPlayer {
src: string,
name: string,
icon: string,
mode: string,
status: string,
duration: number,
position: number,
songAttribute: object,
volume: number,
nextAvailable: bool,
prevAvailable: bool,
play(): void,
pause(): void,
stop(): void,
release(): void,
next(): void,
previous(): void,
requestFocus({acquireType: string, volumeType: string}): void,
releaseFocus(): void,
onplay?: () => void,
onpause?: () => void,
onstop?: () => void,
onended?: () => void,
onerror?: (err: {msg: string})=> void,
ontimeupdate?: () => void,
oninterrupt?: (action: {interruptHint: number}) => void,
onnext?: () => void,
onprevious?: () => void,
onrequestplay?: () => void,
onrequestpause?: () => void,
onrequeststop?: () => void,
onsongattribute?: () => void,
onposition?: () => void,
onrequestfocus?: () => void,
onreleasefocus?: () => void,
onmodechanged?: () => void,
onvolumechange?: () => void,
}
- The
AudioPlayerobject (hereinafter:audiokit.Player) and theAudioPlayerobject created in thesystem.mediamodule (hereinafter:media.Player) are different JS objects, but they manage the same player. Additionally, theaudiokit.Playerobject provides more features than themedia.Playerobject, such asnext(),previous(), and other methods. Operations likeplay()performed via theaudiokit.Playerobject will also notify themedia.Playerobject's listeners.
src
Sets or reads the URL of the audio to be played. Supports local resource paths and network resource paths using http or https protocols (e.g., https://www.rt-thread.com/service/test/001.mp3). Below is a simple example of setting the src and then starting playback:
import audiokit from '@system.audiokit'
// Query the active audio player in the system
let player = audiokit.getActivePlayer()
if (player != null) {
// First stop the audio currently playing
player.stop()
// Set the audio URL to be played
player.src = 'https://www.rt-thread.com/service/test/001.mp3'
// Start playing audio
player.play()
}
name
The name of the player object. If not set, it defaults to the name of the application that created the player. Note that the name of the player object is not globally unique, and the name cannot be used to identify the player object.
icon
The icon URL of the player object. Supports local resource paths
mode
Playback mode. The functionality corresponding to this property should be implemented by the player application; the player object does not handle it by default and only provides this property.
sequential: Sequential playbackrandom: Random playbacksingleloop: Single track looplistloop: List loop
status
Read the current playback status
play: Playing statuspause: Paused statusstop: Stopped statusended: Ended statuserror: Error status
duration
Total audio duration, unit: seconds
position
Current audio playback time position, unit: seconds
songAttribute
Song attribute object
Type Signature
type songAttribute = {
title: string; // Song title
artist: string; // Performer's name, can be an individual or a band
album: string; // Name of the album the song belongs to
year: string; // Release year of the song
genre: string; // Genre of the song, e.g., pop, rock, classical, etc.
track: string; // Track number in the album, e.g., "1/12" means 1st of 12
coverArt: string; // URL of the song's cover art image
lyrics: string; // URL of the lyrics text
comments: string; // Additional information, such as copyright notes, etc.
}
The songAttribute object, like the AudioPlayer object, is a Proxy object, meaning it cannot be serialized or deserialized using JSON, nor can it be referenced in reactive frameworks. Below is a simple usage example:
// Set the song title
this.player.songAttribute.title = "Unknown"
// Set the song artist
this.player.songAttribute.artist = "Unknown"
// Check the song title
console.dir(this.player.songAttribute.title)
volume
The current player volume, range: [0.0, 1.0]
nextAvailable
Sets or queries whether switching to the next track is available
prevAvailable
Sets or queries whether switching to the previous track is available
play
Starts playing the audio specified in the src property
- If the src property is not set before calling this method, playback will fail and trigger the onerror event;
- This method is a synchronous interface. After execution, you need to wait for the onplay or onerror event to determine success or failure. Other operations performed before the event is triggered will be ignored;
Below is a simple example of calling the play() interface:
import audiokit from '@system.audiokit'
// Query the active audio player in the system
let player = audiokit.getActivePlayer()
if (player != null) {
// First stop the currently playing audio
player.stop()
// Set the audio URL to be played
player.src = 'https://www.rt-thread.com/service/test/001.mp3'
// Set the onplay event
player.onplay = () => { console.dir("Start playing") }
// Set the onerror event
player.onerror = () => { console.dir("Playback error") }
// Start playing audio
player.play()
}
pause
Pauses playback of the current audio.
- This method is a synchronous interface. After executing this interface, you need to wait for the
onpauseevent oronerrorevent to determine whether the pause succeeded or failed. Other operations performed before the event is triggered will be ignored.
stop
Stops audio playback. Audio can be replayed via play.
- This method is a synchronous interface. After executing this interface, you need to wait for the
onstopevent oronerrorevent to determine whether the stop succeeded or failed. Other operations performed before the event is triggered will be ignored.
release
Releases audio resources.
- Executing this interface will stop playback of the current audio. You need to wait for the
onstopevent oronerrorevent to determine whether the stop succeeded or failed. Other operations performed before the event is triggered will be ignored.
next
Notifies the player application to play the next track. After executing this interface, the onnext event will be triggered to notify the player application listening to this event, and the player application will execute the song switching logic.
previous
Notifies the player application to play the previous track. After executing this interface, the onprevious event will be triggered to notify the player application listening to this event, and the player application will execute the song switching logic.
requestFocus
Requests audio focus. After executing this interface, it notifies the underlying layer to request or release audio focus, and the underlying layer controls the switching and interruption logic for different types of audio.
The acquireType parameter indicates the request type:
gain: Request audio focusloss: Release audio focus
The volumeType parameter indicates the audio type:
system: System notificationmedia: Media musictts: Text-to-speech (TTS) broadcast
The following example demonstrates how the requestFocus function requests audio focus:
import audiokit from '@system.audiokit'
// Query the active audio player in the system
let player = audiokit.getActivePlayer()
if (player != null) {
// Request audio focus for media music type
player.requestFocus({ volumeType: 'media', acquireType: 'gain' });
}
releaseFocus
Releases audio focus. After calling this interface, the underlying system is notified to release audio focus, and the underlying system controls the switching and interruption logic for different audio types.
onplay
Callback event after audio play succeeds
onpause
Callback event after audio pause succeeds
onstop
Callback event after audio stop succeeds
onended
Callback event after audio playback ends
onerror
Callback event when an error occurs while executing interfaces such as play, pause, stop, or position. When an error occurs, the corresponding events like onplay will not be triggered.
ontimeupdate
Callback event triggered when the position property is updated. This event is only triggered when the application is in the foreground and stops being dispatched when the application is in the background.
oninterrupt
Callback function for audio interruption events. Notification when the current audio is temporarily or completely interrupted by audio of the same or different types.
The interruptHint of the action parameter indicates the type of interruption event:
1: Brief interruption (can automatically resume, e.g., music being interrupted)2: Permanent interruption (cannot automatically resume, e.g., NetEase Cloud Music being interrupted by Ximalaya)
The following example demonstrates how to register the oninterrupt callback function, which is called when the event occurs:
player.oninterrupt = (action) => {
console.log(action.interruptHint)
}
onnext
Callback event when the next track needs to be played.
onprevious
Callback event when the previous track needs to be played.
onrequestplay
This callback event is triggered to notify the JS application when the underlying system needs to start playback. The JS application then executes the logic to start playback.
onrequestpause
This callback event is triggered to notify the JS application when the underlying system needs to pause playback. The JS application then executes the logic to pause playback.
onrequeststop
This callback event is triggered to notify the JS application when the underlying system needs to stop playback. The JS application then executes the logic to stop playback.
onsongattribute
Callback event when the song attribute object changes.
onposition
Callback event when successfully setting the current audio playback time position using position.
onrequestfocus
Callback event when the request for audio focus is successful.
onreleasefocus
Callback event when audio focus is successfully released
onmodechanged
Callback event when the playback mode changes
onvolumechange
Callback event when the player volume changes
