Skip to main content
Version: v3+

Player

Start from API_LEVEL 3.0 . Please refer to API_LEVEL.

Player_image

The media player controller sets audio sources, prepares playback resources, controls playback, and reads media information..

Properties​

source​

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
FILEnumberY-Play a specified audio file3.0

event​

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
PREPAREnumberY-Asynchronous result of prepare()3.0
COMPLETEnumberY-Audio playback completed3.0
PLAYnumberY-Result after start() or resume()3.0
STOPnumberY-Playback stopped3.0
PAUSEnumberY-Playback paused3.0
PROGRESSnumberY-Playback progress updated3.0

state​

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
IDLEnumberY-Initial state3.0
INITIALIZEDnumberY-State after setSource()3.0
PREPARINGnumberY-Intermediate preparing state3.0
PREPAREDnumberY-Resources prepared3.0
STARTINGnumberY-Intermediate starting state3.0
PLAYnumberY-Playback in progress3.0
PAUSINGnumberY-Intermediate pausing state3.0
PAUSEDnumberY-Playback paused3.0
RESUMINGnumberY-Intermediate resuming state3.0

Methods​

setSource​

Set playback parameters and specify the audio file path before playback starts. player.source.FILE supports MP3 files and OPUS files recorded with the audio API. options.file is relative to the mini program assets directory by default; use data:// to access the data directory.

setSource(source: Player['source']['FILE'], options: { file: string }): void

prepare​

Prepare the player by checking the path, file format, and supported bitrate. On success, the player changes state and starts buffering media data; use the event listener to get the result.

prepare(): void

start​

Start playback

start(): void

pause​

Pause playback

pause(): void

resume​

Resume playback; has the same effect as calling start()

resume(): void

stop​

Stop playback

stop(): void

seek​

Start from API_LEVEL 4.2

Set the playback position as a percentage of the total duration. percentage must be in [0 - 100]; the return value indicates whether the position was set successfully.

  • In PREPARED, playback starts automatically after seek()
  • In PAUSED or PLAY, the current state is preserved after seek()
seek(percentage: number): boolean

seekTo​

Start from API_LEVEL 4.3

Set the playback position in seconds; equivalent to seek()

seekTo(seconds: number): boolean

getDuration​

Get the total duration of the current media file in seconds. A return value of 0 is invalid; the player must be in PREPARED to get the duration

getDuration(): number

getVolume​

Get the current system volume in the range [0 - 100]

getVolume(): number

setVolume​

Set the system volume in the range [0 - 100]. The return value indicates whether the setting succeeded; true means success

setVolume(volume: number): boolean

getTitle​

Get the title of the current media file; returns undefined on failure

getTitle(): string | undefined

getArtist​

Get the artist of the current media file; returns undefined on failure

getArtist(): string | undefined

getMediaInfo​

Get the title, artist, and duration of the current media file. title and artist are available after setSource(); duration requires the player to be in PREPARED

getMediaInfo(): {
title: string | undefined
artist: string | undefined
duration: number
}

getStatus​

Get the player state; see player.state for the meanings

getStatus(): number

addEventListener​

Listen for playback state changes; callback is triggered when the state changes

addEventListener(event: number, callback: (result: boolean | number | undefined) => void): void

Example​

import { create, id } from '@zos/media'

const player = create(id.PLAYER)
player.addEventListener(player.event.PREPARE, (result) => {
if (result) player.start()
})
player.addEventListener(player.event.COMPLETE, () => {
player.stop()
})
player.setSource(player.source.FILE, { file: 'data://music.opus' })
const info = player.getMediaInfo()
console.log(info.title, info.artist, info.duration)
player.prepare()
player.pause()
player.resume()
player.stop()