HarmonyOS AudioCapturer录制的音频文件无法播放
import audioUtil from '../common/AudioUtil'
import audio from '@ohos.multimedia.audio';
import { BusinessError } from '@ohos.base';
import fs from '@ohos.file.fs';
class Options {
offset?: number;
length?: number;
}
@Entry
@Component
struct audioTest {
audioCapturer: audio.AudioCapturer | undefined = undefined;
bufferSize: number = 0;
TAG: string = 'audioTest';
aboutToAppear(): void {
this.init()
}
readonly readDataCallback = (buffer: ArrayBuffer) => {
let path = getContext().cacheDir;
let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
let file: fs.File = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let options: Options = {
offset: this.bufferSize,
length: buffer.byteLength
}
fs.writeSync(file.fd, buffer, options);
this.bufferSize += buffer.byteLength;
}
async init() {
// 获取权限
await audioUtil.checkMicPermission()
// 初始化
audio.createAudioCapturer({
streamInfo: {
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采样率
channels: audio.AudioChannel.CHANNEL_2, // 通道
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采样格式
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 编码格式
},
capturerInfo: {
source: audio.SourceType.SOURCE_TYPE_MIC, // 音源类型
capturerFlags: 0 // 音频采集器标志
}
}, (err, capturer) => { // 创建AudioCapturer实例
if (err) {
console.error(`Invoke createAudioCapturer failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info(`${this.TAG}: create AudioCapturer success`);
this.audioCapturer = capturer;
if (this.audioCapturer !== undefined) {
(this.audioCapturer as audio.AudioCapturer).on('readData', this.readDataCallback);
}
});
}
// 开始一次音频采集
start() {
if (this.audioCapturer !== undefined) {
let stateGroup = [audio.AudioState.STATE_PREPARED, audio.AudioState.STATE_PAUSED, audio.AudioState.STATE_STOPPED];
if (stateGroup.indexOf((this.audioCapturer as audio.AudioCapturer).state.valueOf()) === -1) { // 当且仅当状态为STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一时才能启动采集
console.error(`${this.TAG}: start failed`);
return;
}
// 启动采集
(this.audioCapturer as audio.AudioCapturer).start((err: BusinessError) => {
if (err) {
console.error('Capturer start failed.');
} else {
console.info('Capturer start success.');
}
});
}
}
// 停止采集
stop() {
if (this.audioCapturer !== undefined) {
// 只有采集器状态为STATE_RUNNING或STATE_PAUSED的时候才可以停止
if ((this.audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_RUNNING && (this.audioCapturer as audio.AudioCapturer).state.valueOf() !== audio.AudioState.STATE_PAUSED) {
console.info('Capturer is not running or paused');
return;
}
//停止采集
(this.audioCapturer as audio.AudioCapturer).stop((err: BusinessError) => {
if (err) {
console.error('Capturer stop failed.');
} else {
console.info('Capturer stop success.');
}
});
}
}
build() {
Column() {
Button('开始录音')
.onClick(() => {
console.log('charles start')
this.start()
})
.margin({ top: 100 })
Button('结束录音')
.onClick(() => {
console.log('charles stop')
this.stop()
})
}
.width('100%')
.height('100%')
}
}
- 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.
录制完成后的StarWars10s-2C-48000-4SW.wav文件,从IDE的Device File Browser从沙盒中复制到电脑上,使用播放器播放,无法正常播放,显示格式损坏。
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS 通过audio.AudioCapturer录制音频,却没有数据。写入文件,也无法播放
660浏览 • 1回复 待解决
HarmonyOS 播放本地音频文件
1385浏览 • 1回复 待解决
HarmonyOS 获取手机自带系统录制的音频文件列表
1256浏览 • 1回复 待解决
HarmonyOS 音频录制并保存至用户文件目录, 无法播放
718浏览 • 1回复 待解决
HarmonyOS 录制音频选择AudioCapturer还是AVRecorder
1437浏览 • 1回复 待解决
HarmonyOS 使用SoundPool无法播放rawfile下的mp3音频文件
974浏览 • 1回复 待解决
使用AudioCapturer开发音频录制功能
2015浏览 • 1回复 待解决
HarmonyOS AudioCapturer录音后的文件无法播放
604浏览 • 1回复 待解决
HarmonyOS 使用AVPlayer开发音频播放功能,mp3类型的本地音频文件可以正常播放,但是wav类型的音频文件播放失败
739浏览 • 1回复 待解决
HarmonyOS AVPlayer播放本地沙箱目录音频文件
1510浏览 • 1回复 待解决
使用AVPlayer播放音频文件的几种方式实现
3904浏览 • 1回复 待解决
AudioCapturer录音+AudioRenderer播放音频
2377浏览 • 1回复 待解决
HarmonyOS 是否有接口可以获取音频文件的播放时长
874浏览 • 1回复 待解决
HarmonyOS 音频录制与播放
1536浏览 • 1回复 待解决
HarmonyOS 在Worker里面,无法正常写入语音文件,从而无法在Worker中播放音频文件
669浏览 • 1回复 待解决
HarmonyOS AudioCapturer录音后生成的wav文件无法播放
668浏览 • 1回复 待解决
HarmonyOS 音频录制、音频播放功能细节咨询
1313浏览 • 1回复 待解决
HarmonyOS 如何播放resources目录下rawfile目录下的wav音频文件
793浏览 • 1回复 待解决
HarmonyOS 获取本地所有音频文件
808浏览 • 1回复 待解决
HarmonyOS如何从音频文件中获取音频时长
1929浏览 • 1回复 待解决
HarmonyOS 使用AudioCapturer录制的pcm文件如何转化为wav文件?
718浏览 • 1回复 待解决
HarmonyOS 沙箱中wav音频文件播放,停止、暂停API应该怎么写
589浏览 • 1回复 待解决
怎么读取本地音频文件列表?
7312浏览 • 1回复 待解决
HarmonyOS 使用AudioCapturer开发音频录制功能时,可以获取到音频配置最低能录制多少bytes吗?
699浏览 • 1回复 待解决
HarmonyOS如何加载rawfile目录下的音频文件
1284浏览 • 1回复 待解决
audioCapture生成的音频文件是PCM格式原始数据,需要音频输出后添加数据处理才能播放。验证阶段可以把录音文件导出来,用三方软件比如Audacity转为可以播放的格式。