HarmonyOS 使用AudioCapturer录音过程中闪退

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

使用AudioCapturer进行录音操作。

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

HarmonyOS
2024-11-13 10:43:41
1130浏览
收藏 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) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.

​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
微信
回复
2024-11-13 17:11:37


相关问题
HarmonyOS 使用Webview会退
736浏览 • 1回复 待解决
AudioCapturer录音+AudioRenderer播放音频
2377浏览 • 1回复 待解决
HarmonyOS应用退问题
1638浏览 • 1回复 待解决
HarmonyOS 动画过程中UI残留
873浏览 • 1回复 待解决
HarmonyOS number toFixed方法退
924浏览 • 1回复 待解决
HarmonyOS 手势处理高概率退
670浏览 • 1回复 待解决
HarmonyOS 退报错 Error code:2100001
803浏览 • 1回复 待解决
HarmonyOS react-native-webview退问题
932浏览 • 1回复 待解决
HarmonyOS PixelMap.rotate接口导致退
770浏览 • 1回复 待解决
HarmonyOS 页面返回时应用退报错
1685浏览 • 1回复 待解决
HarmonyOS 申请短时后台任务退
798浏览 • 1回复 待解决
页面间传递参数总是退
273浏览 • 0回复 待解决
加载FFMpeg后APP出现退
1099浏览 • 1回复 待解决
HarmonyOS napi开发过程中的泄漏问题
891浏览 • 1回复 待解决
录制过程中HarmonyOS如何切换摄像头
1172浏览 • 1回复 待解决
HarmonyOS升级后,react-native-svg退
761浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。