audio-playback.md 10.0 KB
Newer Older
W
wusongqing 已提交
1
# 音频播放开发指导
M
mamingshuai 已提交
2

W
wusongqing 已提交
3
## 场景介绍
M
mamingshuai 已提交
4

W
wusongqing 已提交
5
音频播放的主要工作是将音频数据转码为可听见的音频模拟信号并通过输出设备进行播放,同时对播放任务进行管理。
M
mamingshuai 已提交
6

W
wusongqing 已提交
7
**图1** 音频播放状态机
8

W
wusongqing 已提交
9
![zh-ch_image_audio_state_machine](figures/zh-ch_image_audio_state_machine.png)
10

Z
zengyawen 已提交
11

12

W
wusongqing 已提交
13
**图2** 音频播放零层图
14

W
wusongqing 已提交
15
![zh-ch_image_audio_player](figures/zh-ch_image_audio_player.png)
16

W
wusongqing 已提交
17
## 开发步骤
18

Z
zengyawen 已提交
19
详细API含义可参考:[媒体服务API文档AudioPlayer](../reference/apis/js-apis-media.md)
20

W
wusongqing 已提交
21
### 全流程场景
22

W
wusongqing 已提交
23
包含流程:创建实例,设置uri,播放音频,跳转播放位置,设置音量,暂停播放,获取轨道信息,停止播放,重置,释放资源等流程。
24

W
wusongqing 已提交
25
AudioPlayer支持的src媒体源输入类型可参考:[src属性说明](../reference/apis/js-apis-media.md#audioplayer_属性)
26 27

```js
28 29 30
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

31
function SetCallBack(audioPlayer) {
W
wusongqing 已提交
32
    audioPlayer.on('dataLoad', () => {              //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
33
        console.info('audio set source success');
W
wusongqing 已提交
34
        //播放界面可切换至已准备好,可点击播放按钮进行播放状态
35
    });
W
wusongqing 已提交
36
    audioPlayer.on('play', () => {                  //设置'play'事件回调
37
        console.info('audio play success');
W
wusongqing 已提交
38
        //将播放按钮切换至可暂停状态
39
    });
W
wusongqing 已提交
40
    audioPlayer.on('pause', () => {                 //设置'pause'事件回调
41
        console.info('audio pause success');
W
wusongqing 已提交
42
        //将播放按钮切换至可播放状态
43
    });
W
wusongqing 已提交
44
    audioPlayer.on('stop', () => {                  //设置'stop'事件回调
45
        console.info('audio stop success');
W
wusongqing 已提交
46
        //播放停止,播放进度条归零,播放按钮切换至可播放状态
47
    });
W
wusongqing 已提交
48
    audioPlayer.on('reset', () => {                 //设置'reset'事件回调
49
        console.info('audio reset success');
W
wusongqing 已提交
50
        //需重新设置src属性后,可继续播放其他音频
51
    });
W
wusongqing 已提交
52
    audioPlayer.on('timeUpdate', (seekDoneTime) => {//设置'timeUpdate'事件回调
53 54
        if (typeof(seekDoneTime) == 'undefined') {
            console.info('audio seek fail');
M
mamingshuai 已提交
55 56
            return;
        }
57
        console.info('audio seek success, and seek time is ' + seekDoneTime);
W
wusongqing 已提交
58
        //播放进度条更新到seek对应的位置
59
    });
W
wusongqing 已提交
60
    audioPlayer.on('volumeChange', () => {          //设置'volumeChange'事件回调
61
        console.info('audio volumeChange success');
W
wusongqing 已提交
62
        //更新音量显示
63
    });
W
wusongqing 已提交
64
    audioPlayer.on('finish', () => {                //设置'finish'事件回调,播放完成触发
65 66
        console.info('audio play finish');
    });
W
wusongqing 已提交
67
    audioPlayer.on('error', (error) => {            //设置'error'事件回调
68 69 70 71 72 73 74 75 76 77 78 79 80 81
        console.info(`audio error called, errName is ${error.name}`);
        console.info(`audio error called, errCode is ${error.code}`);
        console.info(`audio error called, errMessage is ${error.message}`);
    });
}

function printfDescription(obj) {
    for (let item in obj) {
        let property = obj[item];
        console.info('audio key is ' + item);
        console.info('audio value is ' + property);
    }
}

Z
zengyawen 已提交
82
//1. 创建实例
83
let audioPlayer = media.createAudioPlayer();
W
wusongqing 已提交
84
SetCallBack(audioPlayer);                          //设置事件回调
Z
zengyawen 已提交
85
//2. 用户选择音频,设置uri
86 87 88 89 90 91 92 93 94 95 96 97
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});

audioPlayer.src = fdPath;                         //设置src属性,并触发'dataLoad'事件回调
Z
zengyawen 已提交
98
//3. 播放音频
W
wusongqing 已提交
99
audioPlayer.play();                               //需等待'dataLoad'事件回调完成后,才可调用play进行播放,触发'play'事件回调
Z
zengyawen 已提交
100
//4. 跳转播放位置
W
wusongqing 已提交
101
audioPlayer.seek(30000);                          //触发'timeUpdate'事件回调,seek到30000ms处播放
Z
zengyawen 已提交
102
//5. 设置音量
W
wusongqing 已提交
103
audioPlayer.setVolume(0.5);                       //触发'volumeChange'事件回调
Z
zengyawen 已提交
104
//6. 暂停播放
W
wusongqing 已提交
105
audioPlayer.pause();                              //触发'pause'事件回调,暂停播放
Z
zengyawen 已提交
106
//7. 获取轨道信息
W
wusongqing 已提交
107
audioPlayer.getTrackDescription((error, arrlist) => {  //通过回调方式获取音频轨道信息
108 109 110
    if (typeof (arrlist) != 'undefined') {
        for (let i = 0; i < arrlist.length; i++) {
            printfDescription(arrlist[i]);
111
        }
112 113 114 115
    } else {
        console.log(`audio getTrackDescription fail, error:${error.message}`);
    }
});
Z
zengyawen 已提交
116
//8. 停止播放
W
wusongqing 已提交
117
audioPlayer.stop();                              //触发'stop'事件回调
Z
zengyawen 已提交
118
//9. 重置播放资源
W
wusongqing 已提交
119
audioPlayer.reset();                             //触发'reset'事件回调后,重新设置src属性,可完成切歌
Z
zengyawen 已提交
120
//10. 释放资源
W
wusongqing 已提交
121
audioPlayer.release();                           //audioPlayer资源被销毁
122 123 124
audioPlayer = undefined;
```

W
wusongqing 已提交
125
### 正常播放场景
126 127

```js
128 129 130
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

131
function SetCallBack(audioPlayer) {
W
wusongqing 已提交
132
    audioPlayer.on('dataLoad', () => {              //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
133
        console.info('audio set source success');
W
wusongqing 已提交
134
        audioPlayer.play();                         //调用play方法开始播放,触发'play'事件回调
135
    });
W
wusongqing 已提交
136
    audioPlayer.on('play', () => {                  //设置'play'事件回调
137 138
        console.info('audio play success');
    });
W
wusongqing 已提交
139
    audioPlayer.on('finish', () => {                //设置'finish'事件回调,播放完成触发
140
        console.info('audio play finish');
W
wusongqing 已提交
141
        audioPlayer.release();                      //audioPlayer资源被销毁
142 143 144 145
		audioPlayer = undefined;
    });
}

W
wusongqing 已提交
146 147
let audioPlayer = media.createAudioPlayer();       //创建一个音频播放实例
SetCallBack(audioPlayer);                          //设置事件回调
Z
zengyawen 已提交
148
/* 用户选择音频设置fd(本地播放) */
149 150 151 152 153 154 155 156 157 158 159 160
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});

audioPlayer.src = fdPath;                           //设置src属性,并触发'dataLoad'事件回调
161 162
```

W
wusongqing 已提交
163
### 切歌场景
164 165

```js
166 167 168
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

169
function SetCallBack(audioPlayer) {
W
wusongqing 已提交
170
    audioPlayer.on('dataLoad', () => {              //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
171
        console.info('audio set source success');
W
wusongqing 已提交
172
        audioPlayer.play();                         //调用play方法开始播放,触发'play'事件回调
173
    });
W
wusongqing 已提交
174
    audioPlayer.on('play', () => {                  //设置'play'事件回调
175 176
        console.info('audio play success');
    });
W
wusongqing 已提交
177
    audioPlayer.on('finish', () => {                //设置'finish'事件回调,播放完成触发
178
        console.info('audio play finish');
W
wusongqing 已提交
179
        audioPlayer.release();                      //audioPlayer资源被销毁
180 181 182 183
		audioPlayer = undefined;
    });
}

W
wusongqing 已提交
184 185
let audioPlayer = media.createAudioPlayer();       //创建一个音频播放实例
SetCallBack(audioPlayer);                          //设置事件回调
Z
zengyawen 已提交
186
/* 用户选择音频设置fd(本地播放) */
187 188 189 190 191 192 193 194 195 196 197 198
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});

audioPlayer.src = fdPath;                           //设置src属性,并触发'dataLoad'事件回调
W
wusongqing 已提交
199
/* 播放一段时间后,下发切歌指令 */
200
audioPlayer.reset();
201

Z
zengyawen 已提交
202
/* 用户选择音频设置fd(本地播放) */
203 204 205 206 207 208 209 210 211 212 213
let fdNextPath = 'fd://'
let nextPath = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(nextPath).then(fdNumber) => {
   fdNextPath = fdNextPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdNextPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});
audioPlayer.src = fdNextPath;
214 215
```

W
wusongqing 已提交
216
### 单曲循环场景
217 218

```js
219 220 221
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'

222
function SetCallBack(audioPlayer) {
W
wusongqing 已提交
223
    audioPlayer.on('dataLoad', () => {              //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
224
        console.info('audio set source success');
W
wusongqing 已提交
225
        audioPlayer.play();                         //调用play方法开始播放,触发'play'事件回调
226
    });
W
wusongqing 已提交
227
    audioPlayer.on('play', () => {                  //设置'play'事件回调
228 229
        console.info('audio play success');
    });
W
wusongqing 已提交
230
    audioPlayer.on('finish', () => {                //设置'finish'事件回调,播放完成触发
231
        console.info('audio play finish');
W
wusongqing 已提交
232
        audioPlayer.release();                      //audioPlayer资源被销毁
233 234 235 236
		audioPlayer = undefined;
    });
}

W
wusongqing 已提交
237 238
let audioPlayer = media.createAudioPlayer();       //创建一个音频播放实例
SetCallBack(audioPlayer);                          //设置事件回调
239

Z
zengyawen 已提交
240
/* 用户选择音频设置fd(本地播放) */
241 242 243 244 245 246 247 248 249 250 251 252 253
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
   fdPath = fdPath + '' + fdNumber;
   console.info('open fd sucess fd is' + fdPath);
}, (err) => {
   console.info('open fd failed err is' + err);
}),catch((err) => {
   console.info('open fd failed err is' + err);
});

audioPlayer.src = fdPath;                           //设置src属性,并触发'dataLoad'事件回调
audioPlayer.loop = true;                            //设置循环播放属性
W
wusongqing 已提交
254
```