目录

声音音频

接口声明

在manifest.json文件的 features属性中增加如下配置。

{"name": "system.audio"}

导入模块

在调用接口页面的<script>部分增加如下配置。

import audio from '@system.audio'

或

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

使用限制

限制条件

说明

适用终端

手机、平板、智慧屏、车机

适用区域

全球

属性

属性

类型

是否必填

说明

src

string

是

播放的音频媒体的uri。可以设置网络路径或者本地路径,本地路径详细说明参见“文件组织”中的“URI”章节。

currentTime

number

否

音频当前的播放进度,单位秒,对值进行设置可调整播放进度。

duration

number

否

音频的播放时长,单位秒,未知时返回NaN,只读属性,无法设置。

autoplay

boolean

否

音频是否自动播放,缺省为false。

loop

boolean

否

音频是否循环播放,缺省为false。

volume

number

否

音频的音量,0.0 为音量最小,1.0 为最大。

muted

boolean

否

是否静音,true为静音,false不静音,缺省为false。

notificationVisible (1010+)

boolean

否

在进行音频播放时,是否在通知栏中显示音频播放状态,缺省为true。

title (1040+)

string

否

音频标题,未设置时显示正在播放或者暂停播放。

title和artist必须都不为空才会显示,否则两个内容都不会显示。

artist (1040+)

string

否

歌手名,未设置时显示正在播放或者暂停播放。

title和artist必须都不为空才会显示,否则两个内容都不会显示。

cover (1040+)

string

否

封面图片uri路径。封面图片大小为60*60~120*120,宽高比为1:1。

streamType (1035+)

string

否

指定使用音频类型,可设置的值有 music、voicecall,当值为 music 时使用扬声器播放,voicecall 时使用听筒播放。默认为 music。

方法

方法

参数

描述

play

-

开始播放音频。当前支持的格式包括:MP4、M4A、MKV、MP3、Ogg、WAV、MPEG-TS、ADTS格式。

pause

-

暂停播放音频。

stop(1020+)

-

停止音频播放,开发者可以通过play重新播放音乐。

getPlayState(1050+)

-

获取当前播放状态数据。传入参数请参见“getPlayState参数说明”。

getPlayState参数说明:

参数

类型

是否必填

说明

success

function

否

成功回调。

fail

function

否

失败回调。

complete

function

否

执行结束后的回调。

success返回值:

参数

类型

说明

state

string

播放状态,分别为play,pause,stop。

src

string

当前播放的音频媒体 uri,停止时返回空字符串。

currentTime

number

当前音频的播放进度,单位秒,停止时返回-1。

autoplay

boolean

当前音频是否在自动播放。

loop

boolean

当前音频是否在循环播放。

volume

number

当前音频的音量,默认当前系统媒体音量,音量变化范围[0.0,1.0]。

muted

boolean

当前音频是否在静音播放。

notificationVisible

boolean

当前音频是否正在通知栏中显示音频播放状态。

示例代码

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)
    }
})

事件

事件

描述

示例

play

播放开始,未获取到音频数据时的回调事件。

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

pause

播放暂停时的回调事件。

-

loadeddata

获取到音频数据,开始播放时的回调事件。

-

ended

播放结束时的回调事件。

-

durationchange

音频时长变化时的回调事件。

-

error

播放发生错误时的回调事件。

-

timeupdate

播放进度变化时的回调事件,频率为250ms更新一次。

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

previous(1040+)

通知栏点击上一首按钮时触发。

-

next(1040+)

通知栏点击下一首按钮时触发。

-

示例代码

<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>

版本更新说明

版本

发布日期

描述

1050

2019-09-20

新增getPlayState方法,获取当前播放状态数据。

1040

2019-06-24

新增streamType属性,用于设置音频的类型。

1020

2018-06-20

新增stop方法,可实现停止音频的播放。

Tips

相关链接

[h2]FAQ

播放音频时,通知栏有左右箭头控制上一首/下一首,如果页面仅一个播放暂停键,通知栏可以不显示上一首、下一首吗?

[h2]案例