HarmonyOS 鸿蒙Next话筒和喇叭能否同时运行?
HarmonyOS 鸿蒙Next话筒和喇叭能否同时运行?
具体使用场景是什么呢,这两个是可以的同时使用的。可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/mic-management-V5
音频录制可以参考:
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo } from '@kit.CoreFileKit';
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
}
let path = getContext().cacheDir;
let filePath = path + ‘/StarWars10s-2C-48000-4SW.wav’;
let file: fileIo.File = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
let readDataCallback = (buffer: ArrayBuffer) => {
let options: Options = {
offset: bufferSize,
length: buffer.byteLength
}
fileIo.writeSync(file.fd, buffer, options);
bufferSize += buffer.byteLength;
}
// 初始化,创建实例,设置监听事件
function init() {
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;
}
<span class="hljs-comment">// 启动采集</span>
(audioCapturer as audio.AudioCapturer).start((err: BusinessError) => {
<span class="hljs-keyword">if</span> (err) {
console.error(<span class="hljs-string">'Capturer start failed.'</span>);
} <span class="hljs-keyword">else</span> {
console.info(<span class="hljs-string">'Capturer start success.'</span>);
}
});
}
}
// 停止采集
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;
}
<span class="hljs-comment">//停止采集</span>
(audioCapturer as audio.AudioCapturer).stop((err: BusinessError) => {
<span class="hljs-keyword">if</span> (err) {
console.error(<span class="hljs-string">'Capturer stop failed.'</span>);
} <span class="hljs-keyword">else</span> {
fileIo.close(file);
console.info(<span class="hljs-string">'Capturer stop success.'</span>);
}
});
}
}
// 销毁实例,释放资源
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;
}
<span class="hljs-comment">//释放资源</span>
(audioCapturer as audio.AudioCapturer).release((err: BusinessError) => {
<span class="hljs-keyword">if</span> (err) {
console.error(<span class="hljs-string">'Capturer release failed.'</span>);
} <span class="hljs-keyword">else</span> {
console.info(<span class="hljs-string">'Capturer release success.'</span>);
}
});
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
HarmonyOS 鸿蒙Next话筒和喇叭能同时运行。
在HarmonyOS 鸿蒙Next系统中,话筒(麦克风)和喇叭(扬声器)是可以同时运行的。这主要得益于鸿蒙系统的多任务处理能力和音频管理机制。在音频通话、视频会议等场景中,话筒用于录制声音,而喇叭则用于播放声音,两者协同工作以实现双向通信。
此外,鸿蒙系统还提供了丰富的音频API和工具,如AudioRenderer和AudioCapturer,开发者可以利用这些API实现更复杂的音频功能,如音频录制、播放、混音等。
需要注意的是,虽然话筒和喇叭可以同时运行,但在实际应用中还需考虑音频资源的分配和优先级管理,以避免音频冲突和卡顿现象。同时,应用也需具备良好的音频处理能力,以确保音质清晰、流畅。
如果在使用过程中遇到话筒或喇叭无法正常工作的问题,建议检查应用权限设置、音频设备连接状态以及系统音频设置等。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。