HarmonyOS 使用AudioCapturer录音过程中闪退

​参照文档:​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-audio-V5#audiocapturer8

使用AudioCapturer进行录音操作。

经测试:第一次调用api录音正常,在第二次及以后再调用录音api,录音过程中会偶发突然闪退。​

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

​这边本地进行测试,未能复现您的问题,按次序进行初始化录音、开始录音、暂停录音、结束录音,请参考下述demo。

demo1:​

import audio from '@ohos.multimedia.audio'; 
import { BusinessError } from '@ohos.base'; 
import fs from '@ohos.file.fs'; 
 
const TAG = 'AudioCapturerDemo'; 
 
class Options { 
  offset?: number; 
  length?: number; 
} 
 
let context = getContext(this); 
let bufferSize: number = 0; 
let audioCapturer: audio.AudioCapturer | undefined = undefined; 
let audioStreamInfo: audio.AudioStreamInfo = { 
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率 
  channels: audio.AudioChannel.CHANNEL_2, // 通道 
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式 
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式 
} 
let audioCapturerInfo: audio.AudioCapturerInfo = { 
  source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型 
  capturerFlags: 0 // 音频采集器标志 
} 
let audioCapturerOptions: audio.AudioCapturerOptions = { 
  streamInfo: audioStreamInfo, 
  capturerInfo: audioCapturerInfo 
} 
 
const RANDOM_NUM = 10000; 
let path = getContext().cacheDir; 
let filePath: string; 
let file: fs.File; 
 
let readDataCallback = (buffer: ArrayBuffer) => { 
 
  let options: Options = { 
    offset: bufferSize, 
    length: buffer.byteLength 
  } 
  fs.writeSync(file.fd, buffer, options); 
  console.debug("filePath=" + filePath); 
  bufferSize += buffer.byteLength; 
} 
 
// 初始化,创建实例,设置监听事件 
function init() { 
  filePath = path + `${getDate(2)}_${Math.floor(Math.random() * RANDOM_NUM)}.wav` ; 
  file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  audio.createAudioCapturer(audioCapturerOptions, (err, capturer) => { // 创建AudioCapturer实例 
    if (err) { 
      console.error(`Invoke createAudioCapturer failed, code is ${err.code}, message is ${err.message}`); 
      return; 
    } 
    console.info(`${TAG}: create AudioCapturer success`); 
    audioCapturer = capturer; 
    if (audioCapturer !== undefined) { 
      (audioCapturer as audio.AudioCapturer).on('readData', readDataCallback); 
    } 
  }); 
} 
 
// 开始一次音频采集 
function start() { 
  if (audioCapturer !== undefined) { 
    let stateGroup = [audio.AudioState.STATE_PREPARED, audio.AudioState.STATE_PAUSED, audio.AudioState.STATE_STOPPED]; 
    if (stateGroup.indexOf((audioCapturer as audio.AudioCapturer).state.valueOf()) === -1) { // 当且仅当状态为STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一时才能启动采集 
      console.error(`${TAG}: start failed`); 
      return; 
    } 
 
    console.debug("filePath=" + filePath); 
 
    // 启动采集 
    (audioCapturer as audio.AudioCapturer).start((err: BusinessError) => { 
      if (err) { 
        console.error('Capturer start failed.'); 
      } else { 
        console.info('Capturer start success.'); 
      } 
    }); 
  } 
} 
 
// 停止采集 
function stop() { 
  if (audioCapturer !== undefined) { 
    // 只有采集器状态为STATE_RUNNING或STATE_PAUSED的时候才可以停止 
    if ((audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_RUNNING && (audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_PAUSED) { 
      console.info('Capturer is not running or paused'); 
      return; 
    } 
 
    //停止采集 
    (audioCapturer as audio.AudioCapturer).stop((err: BusinessError) => { 
      if (err) { 
        console.error('Capturer stop failed.'); 
      } else { 
        fs.close(file); 
        console.info('Capturer stop success.'); 
      } 
    }); 
  } 
} 
 
// 销毁实例,释放资源 
function release() { 
  if (audioCapturer !== undefined) { 
    // 采集器状态不是STATE_RELEASED或STATE_NEW状态,才能release 
    if ((audioCapturer as audio.AudioCapturer).state.valueOf() === audio.AudioState.STATE_RELEASED || (audioCapturer as audio.AudioCapturer).state.valueOf() === audio.AudioState.STATE_NEW) { 
      console.info('Capturer already released'); 
      return; 
    } 
 
    //释放资源 
    (audioCapturer as audio.AudioCapturer).release((err: BusinessError) => { 
      if (err) { 
        console.error('Capturer release failed.'); 
      } else { 
        console.info('Capturer release success.'); 
      } 
    }); 
  } 
} 
 
function getDate(mode: number): string { 
  let date = new Date(); 
  if (mode === 1) { 
    return `${date.getFullYear()}/${formatNumber(date.getMonth() + 1)}/${formatNumber(date.getDate())}`; 
  } else { 
    return `${date.getFullYear()}${formatNumber(date.getMonth() + 1)}${formatNumber(date.getDate())}`; 
  } 
} 
 
function formatNumber(num: number): string { 
  if (num <= 9) { 
    return '0' + num; 
  } else { 
    return '' + num; 
  } 
} 
 
@Entry 
@Component 
struct Index{ 
  aboutToAppear(): void { 
 
  } 
 
  build() { 
    Column() { 
      Row() { 
        Button('初始化录音') 
          .onClick(() => { 
            init() 
          }) 
      } 
      .width('100%') 
      .margin(50) 
      .justifyContent(FlexAlign.Center) 
      Row() { 
        Button('开始录音') 
          .onClick(() => { 
            start() 
          }) 
      } 
      .width('100%') 
      .margin(50) 
      .justifyContent(FlexAlign.Center) 
      Row() { 
        Button('暂停录音') 
          .onClick(() => { 
            stop() 
          }) 
      } 
      .width('100%') 
      .margin(50) 
      .justifyContent(FlexAlign.Center) 
      Row() { 
        Button('结束录音') 
          .onClick(() => { 
            release() 
          }) 
      } 
      .width('100%') 
      .margin(50) 
      .justifyContent(FlexAlign.Center) 
    } 
    .width('100%') 
    .height('100%') 
    .alignItems(HorizontalAlign.Center) 
    .justifyContent(FlexAlign.Center) 
  } 
}

​demo2:

请参考:​https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Media/Audio#/openharmony/applications_app_samples/blob/master/code/BasicFeature/Media/Audio/entry/src/main/ets/pages/NormalCapturer.ets

分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS应用退问题
487浏览 • 1回复 待解决
AudioCapturer录音+AudioRenderer播放音频
1151浏览 • 1回复 待解决
HarmonyOS 页面返回时应用退报错
350浏览 • 1回复 待解决
录制过程中HarmonyOS如何切换摄像头
325浏览 • 1回复 待解决
mysql存储过程中处理多条数据
1905浏览 • 1回复 待解决
打开图库应用时偶尔会退
2333浏览 • 0回复 待解决
Lite Wearable 开发过程中的疑问
10653浏览 • 2回复 待解决
鸿蒙js开发 文件交互Api 退
6401浏览 • 2回复 待解决
在HAP编译的过程中,本地依赖tgz
730浏览 • 1回复 待解决
HarmonyOS NEXT Developer Beta1模拟器退
1238浏览 • 1回复 待解决
如何处理语音识别过程中的错误?
13浏览 • 0回复 待解决
退和报错日记获取有了解的吗?
2692浏览 • 1回复 待解决