目录

Audio

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.audio"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import audio from '@system.audio'

Or

var audio = require("@system.audio")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, Vision products, and telematics devices

Applicable regions

Global

Attributes

Attribute

Type

Mandatory

Description

src

string

Yes

URI of the audio media to be played. You can set this parameter to a local path or network address. For details about local paths, please refer to URI.

currentTime

number

No

Audio playback progress, in seconds. The playback progress can be adjusted by setting the parameter value.

duration

number

No

Audio duration, in seconds. If the value is unknown, NaN is returned. This parameter is read-only and cannot be set.

autoplay

boolean

No

Indicates whether the audio is automatically played. The default value is false (no).

loop

boolean

No

Indicates whether the audio is played in cyclic mode. The default value is false (no).

volume

number

No

Audio volume. The value ranges from 0.0 to 1.0. A larger value indicates a higher volume.

muted

boolean

No

Indicates whether to mute the audio. The options are true (yes) and false (no). The default value is false.

notificationVisible (1010+)

boolean

No

Indicates whether to display the audio playback status in the notification panel. The default value is true (yes).

title (1040+)

string

No

Audio title. If this parameter is not set, the audio playback status (playing or paused) is displayed.

The value of this parameter is displayed only when both title and artist are set.

artist (1040+)

string

No

Artist name. If this parameter is not set, the audio playback status (playing or paused) is displayed.

The value of this parameter is displayed only when both title and artist are set.

cover (1040+)

string

No

URI of the cover image. The cover image size ranges from 60 x 60 px to 120 x 120 px, and the aspect ratio is 1:1.

streamType (1035+)

string

No

Audio playback type. The options are music and voicecall. When the parameter is set to music, the audio is played using the speaker. When the parameter is set to voicecall, the audio is played using the earpiece. The default value is music.

Methods

Method

Parameter

Description

play

-

Starts audio playback. Currently, the following formats are supported: MP4, M4A, MKV, MP3, Ogg, WAV, MPEG-TS and ADTS.

pause

-

Pauses audio playback.

stop(1020+)

-

Stops audio playback. You can start the audio playback again by calling the play method.

getPlayState(1050+)

-

Obtains the current playback status. For details about the input parameters, please refer to getPlayState parameters.

getPlayState parameters

Parameter

Type

Mandatory

Description

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

state

string

Playback status. The options are play, pause, and stop.

src

string

URI of the audio file that is being played. When the playback stops, an empty string is returned.

currentTime

number

Audio playback progress, in seconds. The value -1 is returned when the playback stops.

autoplay

boolean

Indicates whether the current audio is automatically played.

loop

boolean

Indicates whether the current audio is played cyclically.

volume

number

Audio volume. The default value is the system media volume. The value ranges from 0.0 to 1.0.

muted

boolean

Indicates whether the audio is muted.

notificationVisible

boolean

Indicates whether to display the audio playback status in the notification panel.

Sample code

audio.getPlayState({
    success: function(data) {
        console.log(`handling success: state:{data.state},src:${data.src},currentTime:${data.currentTime},autoplay:${data.autoplay},loop:${data.loop},
            volume: ${data.volume},muted:${data.muted},notificationVisible:${data.notificationVisible}`)
    },
    fail: function(data, code) {
        console.log('handling fail, code=' + code)
    }
})

Events

Event

Description

Example

play

Callback event triggered when audio playback starts but no audio data is obtained.

audio.onplay = function() {
console.log("audio onplay");
};

pause

Callback event triggered when audio playback is paused.

-

loadeddata

Callback event triggered when audio data is obtained and playback starts.

-

ended

Callback event triggered when audio playback stops.

-

durationchange

Callback event triggered when the audio duration changes.

-

error

Callback event triggered when an audio playback error occurs.

-

timeupdate

Callback event triggered when the playback progress changes. Update is performed every 250 ms.

audio.ontimeupdate = function() {
console.log("audio current time:" + audio.currentTime);
};

previous (1040+)

Callback event triggered when the previous song button is tapped in the notification panel.

-

next (1040+)

Callback event triggered when the next song button is tapped in the notification panel.

-

Sample code

<template>
    <div class="container">
        <div class="nav_title">
            <text class="nav_title_detail">{{component_name}}</text>
        </div>
        <div class="item_container">
            <image src="/Common/img/play.png" style="width:50px;height:50px;margin-right:50px;" onclick="play"></image>
            <text>{{time}}</text>
            <text style="margin-left:6px;margin-right:6px;">/</text>
            <text style="margin-right:50px;">{{total}}</text>
            <image src="/Common/img/pause.png" style="width:50px;height:50px;" onclick="pause"></image>
        </div>
        <input type="button" style="background-color:#09ba07;margin-bottom:20px;" onclick="autoPlay" value="autoPlay" />
        <input type="button" style="background-color:#09ba07;margin-bottom:20px;" onclick="manualPlay" value="manualPlay" />
        <input type="button" style="background-color:#09ba07;margin-bottom:20px;" onclick="setMute" value="set muted?" />
        <input type="button" style="background-color:#09ba07;margin-bottom:20px;" onclick="seek" value="seek" />
    </div>
</template>
<style>
    @import '../Common/css/common.css';
    .item_container {
        margin-bottom: 50px;
        margin-right: 60px;
        margin-left: 60px;
        flex-direction: row;
        height: 300px;
    }
    .item {
        height: 250px;
        text-align: center;
        color: #ffffff;
    }
</style>
<script>
    import prompt from '@system.prompt'
    import audio from '@system.audio'
    export default {
        data: {
            component_name: "audio ksong",
            time: 0,
            total: 0,
        },
        autoPlay: function () {
            var that = this;
            var srcUri = 'https://od.qingting.fm/m4a/59db80de7cb8914779254b47_8041831_64.m4a';
            audio.src = srcUri;
            audio.volume = 0.2;
            var isshow = that.notify;
            audio.notificationVisible = isshow;
            audio.autoplay = true;
            audio.loop = true;
            audio.onplay = function () {
                console.log("audio onplay");
            };
            audio.onpause = function () {
                console.log("audio onpause");
            };
            audio.onended = function () {
                console.log("audio onended");
            };
            audio.ondurationchange = function () {
                console.log("audio ondurationchange");
                that.total = audio.duration;
                console.log("audio ondurationchange total=" + that.total);
            };
            audio.ontimeupdate = function () {
                that.time = audio.currentTime;
                console.log("audio ontimeupdate time=" + that.time);
            };
        },
        manualPlay: function () {
            var that = this;
            var s = 'https://od.qingting.fm/m4a/59db80de7cb8914779254b47_8041831_64.m4a';
            audio.src = s;
            audio.play();
            audio.onplay = function () {
                console.log("audio onplay");
            };
            audio.onpause = function () {
                console.log("audio onpause");
            };
            audio.onended = function () {
                console.log("audio onended");
            };
            audio.ondurationchange = function () {
                console.log("audio ondurationchange");
                that.total = audio.duration;
            };
            audio.ontimeupdate = function () {
                console.log("audio ontimeupdate");
                that.time = audio.currentTime;
            };
        },
        play: function () {
            audio.play();
        },
        pause: function () {
            audio.pause();
        },
        setMute: function () {
            var that = this;
            var isMuted = !that.muted;
            that.muted = isMuted;
            audio.muted = isMuted;
        },
        seek: function () {
            audio.currentTime = '288';
        }
    }
</script>

Version Change History

Version

Date

Description

1050

2019-09-20

Added the getPlayState method to obtain the current playback status.

1040

2019-06-24

Added the streamType attribute to set the audio type.

1020

2018-06-20

Added the stop method to stop audio playback.

Tips

Links

[h2]FAQs

When an audio file is being played, the left and right arrows in the notification bar are used to switch to the previous and next songs. If there is only one play/pause button on the page, can the previous and next songs not be displayed in the notification bar?

[h2]Cases