Skip to content

02 音频模块

Xiaomi-Yule edited this page Jul 5, 2021 · 2 revisions

miot/host/audio

音频处理

重要: 请参考 com.xiaomi.demo 中 MHAudioDemo 中各个api的用法。

Export: public
Doc_name: 音频模块
Doc_index: 2
Doc_directory: host
Example

import {Host} from 'miot'
...
Host.audio.startRecord('sample', {'AVFormatIDKey': 'AMR'})
 .then(res => {//start record success})
...
Host.audio.stopRecord().then(res => {//stop finished})
...

miot/host/audio~IAudio

Kind: inner interface of miot/host/audio


iAudio.isAbleToRecord() ⇒ boolean

用户是否开启录制权限 在Android平台下 由于需要动态获取录音权限,所以该方法固定返回true,但是并不意味着可以录音。

Kind: instance method of IAudio


iAudio.startRecord(audioName, settings) ⇒ Promise

开始录音 在Android平台下 由于需要动态获取录音权限 使用方法请参考 请参考 com.xiaomi.demo 中 MHAudioDemo 的用法

Kind: instance method of IAudio

Param Type Description
audioName string 保存文件名,如 audio.mp3
settings json 配置参数{ RecordType: 录制类型,可选值有 audioRecord,mediaRecord; only worked for android AVSampleRateKey 采样率 默认44100, AVNumberOfChannelsKey 声道,默认2, AVLinearPCMBitDepthKey 音频编码比特率 默认16, AVFormatIDKey 编码格式(AMR,AMR_WB,MPEG4AAC,MPEG4CELP,MPEG4HVXC,MPEG4TwinVQ,AC3,60958AC3 ; recordType为audioRecord时,可以指定录制G711格式音频,设置AVFormatIDKey即可,默认是pcm裸数据), 仅仅适用于Android端,且不支持AAC; AVEncoderAudioQualityKey 音质(Min,Low,Medium,High,Max) }

Example

import { Host } from "miot";
import React from 'react';
import { PermissionsAndroid, Platform } from 'react-native';

var settings = {
  RecordType: 'mediaRecord'
  AVFormatIDKey: 'audioFormatLinearPCM',
  AVSampleRateKey: 9500,
  AVNumberOfChannelsKey: 2,
  AVEncoderAudioQualityKey: 'audioQualityHigh',
  AVLinearPCMBitDepthKey: 16,
  AVLinearPCMIsBigEndianKey: false,
  AVLinearPCMIsFloatKey: false,
};
if (Platform.OS === 'android') {
  PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, null)
    .then((granted) => {
      console.log("granted", granted);
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        Host.audio.startRecord(fileName, settings).then(() => {
          console.log('startRecord');
        });
      }
    }).catch((error) => {
      console.log("error", error)
    })
} else {
  Host.audio.startRecord(fileName, settings).then(() => {
    console.log('startRecord');
  }).catch((err) => {
    console.log('startRecord catch error' + err);
  });
}

iAudio.stopRecord() ⇒ Promise

停止录音

Kind: instance method of IAudio


iAudio.startPlay(audioName, settings) ⇒ Promise

开始播放

Kind: instance method of IAudio

Param Type Description
audioName string 保存文件名,如 audio.mp3
settings json 配置参数{ playerType: 播放器类型,可选值有 audioTrack,mediaPlayer; only worked for android AVSampleRateKey 采样率 默认44100, AVNumberOfChannelsKey 声道,默认2, AVLinearPCMBitDepthKey 音频编码比特率 默认16, AVFormatIDKey 编码格式(AMR,AMR_WB,MPEG4AAC,MPEG4CELP,MPEG4HVXC,MPEG4TwinVQ,AC3,60958AC3 ; playerType为audioTrack时,可以指定播放G711格式音频,设置AVFormatIDKey即可,默认是pcm裸数据), 仅仅适用于Android端,且不支持AAC; AVEncoderAudioQualityKey 音质(Min,Low,Medium,High,Max) audioPlayerUid 播放器ID,监听播放进度的时候会用到 }

Example

let params = {
  'playerType':'audioTrack',
  AVFormatIDKey:'G711',
  'updateAudioPlayerTimeInterval': 1,
  'audioPlayerUid': 'audioPlayerUid'
};
Host.audio.startPlay(fileName, params).then(() => { console.log('startPlay'); })

iAudio.stopPlay() ⇒ Promise

停止播放

Kind: instance method of IAudio


iAudio.getRecordingPeakPower() ⇒ Promise

获取当前录制声音的峰值声音强度。 for iOS: 对应的原生api为 [AVAudioRecorder peakPowerForChannel:0] iOS官方文档:https://developer.apple.com/documentation/avfoundation/avaudiorecorder/1389463-peakpowerforchannel?language=objc 返回值为0 表示满刻度或最大;返回值为-160表示最小(即接近静默)。 取值范围是 -160 - 0, -160 意味着接近silence(静音),0 表示 最大的可测强度 但是实际在测试过程中发现会大于0的输出情况,该值在测试极限情况出现过 10.5 的情况

for android: 取值范围是0-2^15,对应的原生api 为 MediaRecorder.getMaxAmplitude(),仅RecordType为mediaRecord时可用

Kind: instance method of IAudio
Returns: Promise - 成功时:{"code":0, "data":xxx} 失败时:{"code":-1, "message":"xxx" }
Since: 10030


iAudio.wavToAmr(wavPath, savePath) ⇒ Promise

wav转 amr android暂不支持该方法

Kind: instance method of IAudio

Param Type Description
wavPath string 读取 wav 文件名
savePath string 保存 amr 文件名

iAudio.amrToWav(amrPath, savePath) ⇒ Promise

amr 转 wav android暂不支持该方法

Kind: instance method of IAudio

Param Type Description
amrPath string 读取 amr 文件名
savePath string 保存 wav 文件名

miot/host/audio~AudioEvent : object

Audio播放事件名集合

Kind: inner namespace of miot/host/audio
Example

import { AudioEvent } from 'miot/host/audio';
   const subscription = AudioEvent.audioPlayerDidFinishPlaying.addListener(
      (event)=>{
         ...
      }
    )
   ...
   subscription.remove()
   ...

"audioPlayerDidFinishPlaying" (event)

播放完毕事件

Kind: event emitted by AudioEvent
Since: 10020

Param Type Description
event json {audioPlayerUid,isSuccess}音频播放的Uid,是否播放成功

"updateAudioPlayerTime" (params)

播放进度事件

Kind: event emitted by AudioEvent
Since: 10020

Param Type Description
params json {audioPlayerUid,currentTime}音频播放的Uid,播放当前进度

"audioPlayerDidStartPlaying" (event)

播放开始事件

Kind: event emitted by AudioEvent
Since: 10020

Param Type Description
event json {audioPlayerUid,isSuccess}音频播放的Uid,是否成功开始播放

Clone this wiki locally