未验证 提交 efe09394 编写于 作者: O openharmony_ci 提交者: Gitee

!3522 API 9 Canary文档合并

Merge pull request !3522 from zengyawen/OpenHarmony-3.1-Release
......@@ -28,26 +28,37 @@ AudioPlayer支持的src媒体源输入类型可参考:[src属性说明](../ref
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) {
//打印码流轨道信息
function printfDescription(obj) {
for (let item in obj) {
let property = obj[item];
console.info('audio key is ' + item);
console.info('audio value is ' + property);
}
}
//设置播放器回调函数
function setCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console.info('audio set source success');
//播放界面可切换至已准备好,可点击播放按钮进行播放状态
audioPlayer.play(); //需等待'dataLoad'事件回调完成后,才可调用play进行播放,触发'play'事件回调
});
audioPlayer.on('play', () => { //设置'play'事件回调
console.info('audio play success');
//将播放按钮切换至可暂停状态
audioPlayer.pause(); //触发'pause'事件回调,暂停播放
});
audioPlayer.on('pause', () => { //设置'pause'事件回调
console.info('audio pause success');
//将播放按钮切换至可播放状态
audioPlayer.seek(5000); //触发'timeUpdate'事件回调,seek到5000ms处播放
});
audioPlayer.on('stop', () => { //设置'stop'事件回调
console.info('audio stop success');
//播放停止,播放进度条归零,播放按钮切换至可播放状态
audioPlayer.reset(); //触发'reset'事件回调后,重新设置src属性,可完成切歌
});
audioPlayer.on('reset', () => { //设置'reset'事件回调
console.info('audio reset success');
//需重新设置src属性后,可继续播放其他音频
audioPlayer.release(); //audioPlayer资源被销毁
audioPlayer = undefined;
});
audioPlayer.on('timeUpdate', (seekDoneTime) => {//设置'timeUpdate'事件回调
if (typeof(seekDoneTime) == 'undefined') {
......@@ -55,11 +66,20 @@ function SetCallBack(audioPlayer) {
return;
}
console.info('audio seek success, and seek time is ' + seekDoneTime);
//播放进度条更新到seek对应的位置
audioPlayer.setVolume(0.5); //触发'volumeChange'事件回调
});
audioPlayer.on('volumeChange', () => { //设置'volumeChange'事件回调
console.info('audio volumeChange success');
//更新音量显示
audioPlayer.getTrackDescription((error, arrlist) => { //通过回调方式获取音频轨道信息
if (typeof (arrlist) != 'undefined') {
for (let i = 0; i < arrlist.length; i++) {
printfDescription(arrlist[i]);
}
} else {
console.log(`audio getTrackDescription fail, error:${error.message}`);
}
audioPlayer.stop(); //触发'stop'事件回调,停止播放
});
});
audioPlayer.on('finish', () => { //设置'finish'事件回调,播放完成触发
console.info('audio play finish');
......@@ -71,55 +91,24 @@ function SetCallBack(audioPlayer) {
});
}
function printfDescription(obj) {
for (let item in obj) {
let property = obj[item];
console.info('audio key is ' + item);
console.info('audio value is ' + property);
}
}
//1. 创建实例
let audioPlayer = media.createAudioPlayer();
SetCallBack(audioPlayer); //设置事件回调
//2. 用户选择音频,设置uri
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
async function audioPlayerDemo() {
// 1. 创建实例
let audioPlayer = media.createAudioPlayer();
setCallBack(audioPlayer); //设置事件回调
//2. 用户选择音频,设置uri
let fdPath = 'fd://'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3';
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
}).catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
//3. 播放音频
audioPlayer.play(); //需等待'dataLoad'事件回调完成后,才可调用play进行播放,触发'play'事件回调
//4. 跳转播放位置
audioPlayer.seek(30000); //触发'timeUpdate'事件回调,seek到30000ms处播放
//5. 设置音量
audioPlayer.setVolume(0.5); //触发'volumeChange'事件回调
//6. 暂停播放
audioPlayer.pause(); //触发'pause'事件回调,暂停播放
//7. 获取轨道信息
audioPlayer.getTrackDescription((error, arrlist) => { //通过回调方式获取音频轨道信息
if (typeof (arrlist) != 'undefined') {
for (let i = 0; i < arrlist.length; i++) {
printfDescription(arrlist[i]);
}
} else {
console.log(`audio getTrackDescription fail, error:${error.message}`);
}
});
//8. 停止播放
audioPlayer.stop(); //触发'stop'事件回调
//9. 重置播放资源
audioPlayer.reset(); //触发'reset'事件回调后,重新设置src属性,可完成切歌
//10. 释放资源
audioPlayer.release(); //audioPlayer资源被销毁
audioPlayer = undefined;
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
}
```
### 正常播放场景
......@@ -127,8 +116,9 @@ audioPlayer = undefined;
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) {
export class AudioDemo {
// 设置播放器回调函数
setCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console.info('audio set source success');
audioPlayer.play(); //调用play方法开始播放,触发'play'事件回调
......@@ -141,23 +131,25 @@ function SetCallBack(audioPlayer) {
audioPlayer.release(); //audioPlayer资源被销毁
audioPlayer = undefined;
});
}
}
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
SetCallBack(audioPlayer); //设置事件回调
/* 用户选择音频设置fd(本地播放) */
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
async audioPlayerDemo() {
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
this.setCallBack(audioPlayer); //设置事件回调
let fdPath = 'fd://'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3';
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
}).catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
}
}
```
### 切歌场景
......@@ -165,52 +157,62 @@ audioPlayer.src = fdPath; //设置src属性,并触
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) {
export class AudioDemo {
// 设置播放器回调函数
private isNextMusic = false;
setCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console.info('audio set source success');
audioPlayer.play(); //调用play方法开始播放,触发'play'事件回调
});
audioPlayer.on('play', () => { //设置'play'事件回调
console.info('audio play success');
audioPlayer.reset(); //调用reset方法,触发'reset'事件回调
});
audioPlayer.on('finish', () => { //设置'finish'事件回调,播放完成触发
console.info('audio play finish');
audioPlayer.on('reset', () => { //设置'reset'事件回调
console.info('audio play success');
if (!this.isNextMusic) { //当isNextMusic 为false时,实现切歌功能
this.nextMusic(audioPlayer); //实现切歌功能
} else {
audioPlayer.release(); //audioPlayer资源被销毁
audioPlayer = undefined;
}
});
}
}
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
SetCallBack(audioPlayer); //设置事件回调
/* 用户选择音频设置fd(本地播放) */
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) => {
async nextMusic(audioPlayer) {
this.isNextMusic = true;
let nextFdPath = 'fd://'
// path路径的码流可通过"hdc file send D:\xxx\02.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let nextpath = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/02.mp3';
await fileIO.open(nextpath).then((fdNumber) => {
nextFdPath = nextFdPath + '' + fdNumber;
console.info('open fd sucess fd is' + nextFdPath);
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
}).catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
/* 播放一段时间后,下发切歌指令 */
audioPlayer.reset();
});
audioPlayer.src = nextFdPath; //设置src属性,并重新触发触发'dataLoad'事件回调
}
/* 用户选择音频设置fd(本地播放) */
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) => {
async audioPlayerDemo() {
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
this.setCallBack(audioPlayer); //设置事件回调
let fdPath = 'fd://'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/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) => {
}).catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdNextPath;
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
}
}
```
### 单曲循环场景
......@@ -218,39 +220,36 @@ audioPlayer.src = fdNextPath;
```js
import media from '@ohos.multimedia.media'
import fileIO from '@ohos.fileio'
function SetCallBack(audioPlayer) {
export class AudioDemo {
// 设置播放器回调函数
setCallBack(audioPlayer) {
audioPlayer.on('dataLoad', () => { //设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console.info('audio set source success');
audioPlayer.loop = true; //设置循环播放属性
audioPlayer.play(); //调用play方法开始播放,触发'play'事件回调
});
audioPlayer.on('play', () => { //设置'play'事件回调
audioPlayer.on('play', () => { //设置'play'事件回调,开始循环播放
console.info('audio play success');
});
audioPlayer.on('finish', () => { //设置'finish'事件回调,播放完成触发
console.info('audio play finish');
audioPlayer.release(); //audioPlayer资源被销毁
audioPlayer = undefined;
});
}
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
SetCallBack(audioPlayer); //设置事件回调
}
/* 用户选择音频设置fd(本地播放) */
let fdPath = 'fd://'
let path = 'data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp3';
await fileIO.open(path).then(fdNumber) => {
async audioPlayerDemo() {
let audioPlayer = media.createAudioPlayer(); //创建一个音频播放实例
this.setCallBack(audioPlayer); //设置事件回调
let fdPath = 'fd://'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3';
await fileIO.open(path).then((fdNumber) => {
fdPath = fdPath + '' + fdNumber;
console.info('open fd sucess fd is' + fdPath);
}, (err) => {
}, (err) => {
console.info('open fd failed err is' + err);
}),catch((err) => {
}).catch((err) => {
console.info('open fd failed err is' + err);
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
audioPlayer.loop = true; //设置循环播放属性
});
audioPlayer.src = fdPath; //设置src属性,并触发'dataLoad'事件回调
}
}
```
## 相关实例
......@@ -258,7 +257,6 @@ audioPlayer.loop = true; //设置循环播放属性
针对音频播放开发,有以下相关实例可供参考:
- [`JsDistributedMusicPlayer`:分布式音乐播放(JS)(API7)](https://gitee.com/openharmony/app_samples/tree/master/ability/JsDistributedMusicPlayer)
- [`JsAudioPlayer`:音频播放和管理(JS)(API7)](https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer)
- [`eTsAudioPlayer`: 音频播放器(eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)
- [音频播放器](https://gitee.com/openharmony/codelabs/tree/master/Media/Audio_OH_ETS)
\ No newline at end of file
......@@ -25,46 +25,49 @@
```js
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class AudioRecorderDemo {
private testFdNumber; // 用于保存fd地址
let testFdNumber;
function SetCallBack(audioRecorder) {
// 设置音频录制相关回调函数
setCallBack(audioRecorder) {
audioRecorder.on('prepare', () => { // 设置'prepare'事件回调
console.log('prepare success');
// 录制界面可切换至已准备好,可点击录制按钮进行录制
audioRecorder.start(); // 调用start方法开始录制,并触发start回调
});
audioRecorder.on('start', () => { // 设置'start'事件回调
console.log('audio recorder start success');
// 将录制按钮切换至可暂停状态
audioRecorder.pause(); // 调用pause方法暂停录制,并触发pause回调
});
audioRecorder.on('pause', () => { // 设置'pause'事件回调
console.log('audio recorder pause success');
// 将录制按钮切换至可录制状态
audioRecorder.resume(); // 调用resume方法恢复录制,并触发resume回调
});
audioRecorder.on('resume', () => { // 设置'resume'事件回调
console.log('audio recorder resume success');
// 将录制按钮切换至可暂停状态
audioRecorder.stop(); // 调用stop方法停止录制,并触发stop回调
});
audioRecorder.on('stop', () => { // 设置'stop'事件回调
console.log('audio recorder stop success');
});
audioRecorder.on('release', () => { // 设置'release'事件回调
console.log('audio recorder release success');
audioRecorder.reset(); // 调用reset方法重置录制,并触发reset回调
});
audioRecorder.on('reset', () => { // 设置'reset'事件回调
console.log('audio recorder reset success');
// 需要重新设置录制参数才能再次录制
audioRecorder.release(); // 调用release方法,释放资源,并触发release回调
});
audioRecorder.on('release', () => { // 设置'release'事件回调
console.log('audio recorder release success');
audioRecorder = undefined;
});
audioRecorder.on('error', (error) => { // 设置'error'事件回调
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}`);
});
}
}
// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Movies/01.mp3
// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA
async function getFd(pathName) {
// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Video/01.mp3
// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA
async getFd(pathName) {
let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey;
......@@ -80,41 +83,29 @@ async function getFd(pathName) {
let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber;
this.testFdNumber = "fd://" + fdNumber.toString();
}
}
}
await getFd('01.mp3');
// 1.创建实例
let audioRecorder = media.createAudioRecorder();
// 2.设置回调
SetCallBack(audioRecorder);
// 3.设置录制参数
let audioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC ,
async audioRecorderDemo() {
// 1.创建实例
let audioRecorder = media.createAudioRecorder();
// 2.设置回调
this.setCallBack(audioRecorder);
await this.getFd('01.mp3'); // 调用getFd方法获取需要录制文件的fd地址
// 3.设置录制参数
let audioRecorderConfig = {
audioEncodeBitRate : 22050,
audioSampleRate : 22050,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : testFdNumber, // testFdNumber由getFd生成
uri : this.testFdNumber, // testFdNumber由getFd生成
location : { latitude : 30, longitude : 130},
audioEncoderMime : media.CodecMimeType.AUDIO_AAC,
fileFormat : media.ContainerFormatType.CFT_MPEG_4A,
}
audioRecorder.prepare(audioRecorderConfig); // 调用prepare方法,触发prepare回调函数
}
}
audioRecorder.prepare(audioRecorderConfig);
// 4.开始录制
audioRecorder.start(); // 需等待'prepare'事件回调完成后,才可调用start进行录制,触发'start'事件回调
// 5.暂停录制
audioRecorder.pause(); // 需等待'start'事件回调完成后,才可调用pause进行暂停,触发'pause'事件回调
// 6.恢复录制
audioRecorder.resume(); // 需等待'pause'事件回调完成后,才可调用resume进行录制,触发'resume'事件回调
// 7.停止录制
audioRecorder.stop(); // 需等待'start'或'resume'事件回调完成后,才可调用stop进行暂停,触发'stop'事件回调
// 8.重置录制
audioRecorder.reset(); // 触发'reset'事件回调后,重新进行prepare,才可重新录制
// 9.释放资源
audioRecorder.release(); // audioRecorder资源被销毁
audioRecorder = undefined;
```
### 正常录制场景
......@@ -124,29 +115,37 @@ audioRecorder = undefined;
```js
import media from '@ohos.multimedia.media'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
export class AudioRecorderDemo {
private testFdNumber; // 用于保存fd地址
let testFdNumber;
function SetCallBack(audioRecorder) {
// 设置音频录制相关回调函数
setCallBack(audioRecorder) {
audioRecorder.on('prepare', () => { // 设置'prepare'事件回调
console.log('prepare success');
// 录制界面可切换至已准备好,可点击录制按钮进行录制
audioRecorder.start(); // 调用start方法开始录制,并触发start回调
});
audioRecorder.on('start', () => { // 设置'start'事件回调
console.log('audio recorder start success');
// 将录制按钮切换至可暂停状态
audioRecorder.stop(); // 调用stop方法停止录制,并触发stop回调
});
audioRecorder.on('stop', () => { // 设置'stop'事件回调
console.log('audio recorder stop success');
audioRecorder.release(); // 调用release方法,释放资源,并触发release回调
});
audioRecorder.on('release', () => { // 设置'release'事件回调
console.log('audio recorder release success');
audioRecorder = undefined;
});
}
audioRecorder.on('error', (error) => { // 设置'error'事件回调
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}`);
});
}
// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Movies/01.mp3
// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA
async function getFd(pathName) {
// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Video/01.mp3
// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA
async getFd(pathName) {
let displayName = pathName;
const mediaTest = mediaLibrary.getMediaLibrary();
let fileKeyObj = mediaLibrary.FileKey;
......@@ -162,35 +161,29 @@ async function getFd(pathName) {
let fetchFileResult = await mediaTest.getFileAssets(fetchOp);
let fileAsset = await fetchFileResult.getAllObject();
let fdNumber = await fileAsset[0].open('Rw');
fdNumber = "fd://" + fdNumber.toString();
testFdNumber = fdNumber;
this.testFdNumber = "fd://" + fdNumber.toString();
}
}
}
await getFd('01.mp3');
// 1.创建实例
let audioRecorder = media.createAudioRecorder();
// 2.设置回调
SetCallBack(audioRecorder);
// 3.设置录制参数
let audioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC ,
async audioRecorderDemo() {
// 1.创建实例
let audioRecorder = media.createAudioRecorder();
// 2.设置回调
this.setCallBack(audioRecorder);
await this.getFd('01.mp3'); // 调用getFd方法获取需要录制文件的fd地址
// 3.设置录制参数
let audioRecorderConfig = {
audioEncodeBitRate : 22050,
audioSampleRate : 22050,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : testFdNumber, // testFdNumber由getFd生成
uri : this.testFdNumber, // testFdNumber由getFd生成
location : { latitude : 30, longitude : 130},
audioEncoderMime : media.CodecMimeType.AUDIO_AAC,
fileFormat : media.ContainerFormatType.CFT_MPEG_4A,
}
audioRecorder.prepare(audioRecorderConfig); // 调用prepare方法,触发prepare回调函数
}
}
audioRecorder.prepare(audioRecorderConfig)
// 4.开始录制
audioRecorder.start(); // 需等待'prepare'事件回调完成后,才可调用start进行录制,触发'start'事件回调
// 5.停止录制
audioRecorder.stop(); // 需等待'start'或'resume'事件回调完成后,才可调用stop进行暂停,触发'stop'事件回调
// 6.释放资源
audioRecorder.release(); // audioRecorder资源被销毁
audioRecorder = undefined;
```
## 相关实例
......@@ -198,5 +191,5 @@ audioRecorder = undefined;
针对音频录制开发,有以下相关实例可供参考:
- [`Recorder`:录音机(eTS)(API8)](https://gitee.com/openharmony/app_samples/tree/master/media/Recorder)
- [`eTsAudioPlayer`: 音频播放器(eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)
- [音频播放器](https://gitee.com/openharmony/codelabs/tree/master/Media/Audio_OH_ETS)
......@@ -4,10 +4,12 @@
图片开发的主要工作是将获取到的图片进行解码,将解码后的pixelmap编码成支持的格式,本文将对图片的解码、编码等场景开发进行介绍说明。
## 开发步骤
## 接口说明
详细API含义请参考:[图片处理API文档](../reference/apis/js-apis-image.md)
## 开发步骤
### 全流程场景
包含流程:创建实例,读取图片信息,读写pixelmap,更新数据,打包像素,释放资源等流程。
......
......@@ -2,6 +2,8 @@
> **说明:**
> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>
> API Version 9当前为Canary版本,仅供试用,不保证接口可稳定调用。
该模块提供以下音频相关的常用功能:
......@@ -435,6 +437,17 @@ audio.createAudioRenderer(audioCapturerOptions).then((data) => {
| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断事件开始。 |
| INTERRUPT_TYPE_END | 2 | 音频播放中断事件结束。 |
## InterruptForceType<sup>9+</sup>
枚举,强制打断类型。
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer
| 名称 | 默认值 | 描述 |
| --------------- | ------ | ------------------------------------ |
| INTERRUPT_FORCE | 0 | 由系统进行操作,强制打断音频播放。 |
| INTERRUPT_SHARE | 1 | 由应用进行操作,可以选择打断或忽略。 |
## InterruptHint
枚举,中断提示。
......@@ -499,6 +512,18 @@ audio.createAudioRenderer(audioCapturerOptions).then((data) => {
| streamInfo | [AudioStreamInfo](#audiostreaminfo8) | 是 | 表示音频流信息。 |
| rendererInfo | [AudioRendererInfo](#audiorendererinfo8) | 是 | 表示渲染器信息。 |
## InterruptEvent<sup>9+</sup>
播放中断时,应用接收的中断事件。
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Renderer
| 名称 | 类型 | 必填 | 说明 |
| --------- | ------------------------------------------ | ---- | ------------------------------------ |
| eventType | [InterruptType](#interrupttype) | 是 | 中断事件类型,开始或是结束。 |
| forceType | [InterruptForceType](#interruptforcetype9) | 是 | 操作是由系统执行或是由应用程序执行。 |
| hintType | [InterruptHint](#interrupthint) | 是 | 中断提示。 |
## AudioInterrupt
音频监听事件传入的参数。
......@@ -2475,6 +2500,51 @@ audioRenderer.getRenderRate().then((renderRate) => {
});
```
### on('interrupt')<sup>9+</sup>
on(type: 'interrupt', callback: Callback\<InterruptEvent>): void
监听音频中断事件。使用callback获取中断事件。
**系统能力**: SystemCapability.Multimedia.Audio.Renderer
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 事件回调类型,支持的事件为:'interrupt'(中断事件被触发,音频播放被中断。) |
| callback | Callback<[InterruptEvent](#interruptevent9)> | 是 | 被监听的中断事件的回调。 |
**示例:**
```
audioRenderer.on('interrupt', (interruptEvent) => {
if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) {
switch (interruptEvent.hintType) {
case audio.InterruptHint.INTERRUPT_HINT_PAUSE:
console.log('Force paused. Stop writing');
isPlay = false;
break;
case audio.InterruptHint.INTERRUPT_HINT_STOP:
console.log('Force stopped. Stop writing');
isPlay = false;
break;
}
} else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) {
switch (interruptEvent.hintType) {
case audio.InterruptHint.INTERRUPT_HINT_RESUME:
console.log('Resume force paused renderer or ignore');
startRenderer();
break;
case audio.InterruptHint.INTERRUPT_HINT_PAUSE:
console.log('Choose to pause or ignore');
pauseRenderer();
break;
}
}
});
```
### on('markReach')<sup>8+</sup>
on(type: 'markReach', frame: number, callback: (position: number) => {}): void
......
此差异已折叠。
......@@ -2,6 +2,8 @@
> **说明:**
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>
> API Version 9当前为Canary版本,仅供试用,不保证接口可稳定调用。
## 导入模块
......@@ -990,14 +992,40 @@ release(): Promise\<void>
| RGBA_8888 | 3 | 格式为RGBA_8888。 |
| RGB_565 | 2 | 格式为RGB_565。 |
## AlphaType<sup>9+</sup>
枚举,透明度。
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Image
| 名称 | 默认值 | 描述 |
| -------- | ------ | ----------------------- |
| UNKNOWN | 0 | 未知透明度。 |
| OPAQUE | 1 | 没有alpha或图片全透明。 |
| PREMUL | 2 | RGB前乘alpha。 |
| UNPREMUL | 3 | RGB不前乘alpha。 |
## ScaleMode<sup>9+</sup>
枚举,缩略值。
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Image
| 名称 | 默认值 | 描述 |
| --------------- | ------ | -------------------------------------------------- |
| CENTER_CROP | 1 | 缩放图像以填充目标图像区域并居中裁剪区域外的效果。 |
| FIT_TARGET_SIZE | 2 | 图像适合目标尺寸的效果。 |
## InitializationOptions<sup>8+</sup>
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.Image
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ----------- | ---------------------------------- | ---- | ---- | -------------- |
| alphaType<sup>9+</sup> | [AlphaType](#alphatype9) | 是 | 是 | 透明度。 |
| editable | boolean | 是 | 是 | 是否可编辑。 |
| pixelFormat | [PixelMapFormat](#pixelmapformat7) | 是 | 是 | 像素格式。 |
| scaleMode<sup>9+</sup> | [ScaleMode](#scalemode9) | 是 | 是 | 缩略值。 |
| size | [Size](#size) | 是 | 是 | 创建图片大小。 |
## DecodingOptions<sup>7+</sup>
......@@ -1058,7 +1086,7 @@ release(): Promise\<void>
| 名称 | 默认值 | 说明 |
| ----------------- | ----------------- | -------------------- |
| BITS_PER_SAMPLE | "BitsPerSample" | 每个像素字节数。 |
| BITS_PER_SAMPLE | "BitsPerSample" | 每个像素比特数。 |
| ORIENTATION | "Orientation" | 图片方向。 |
| IMAGE_LENGTH | "ImageLength" | 图片长度。 |
| IMAGE_WIDTH | "ImageWidth" | 图片宽度。 |
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册