W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在音頻通話場景下,音頻輸出(播放對端聲音)和音頻輸入(錄制本端聲音)會(huì)同時(shí)進(jìn)行,應(yīng)用可以通過使用AudioRenderer來實(shí)現(xiàn)音頻輸出,通過使用AudioCapturer來實(shí)現(xiàn)音頻輸入,同時(shí)使用AudioRenderer和AudioCapturer即可實(shí)現(xiàn)音頻通話功能。
在音頻通話開始和結(jié)束時(shí),應(yīng)用可以自行檢查當(dāng)前的音頻場景模式和鈴聲模式,以便采取合適的音頻管理及提示策略。
以下代碼示范了同時(shí)使用AudioRenderer和AudioCapturer實(shí)現(xiàn)音頻通話功能的基本過程,其中未包含音頻通話數(shù)據(jù)的傳輸過程,實(shí)際開發(fā)中,需要將網(wǎng)絡(luò)傳輸來的對端通話數(shù)據(jù)解碼播放,此處僅以讀取音頻文件的數(shù)據(jù)代替;同時(shí)需要將本端錄制的通話數(shù)據(jù)編碼打包,通過網(wǎng)絡(luò)發(fā)送給對端,此處僅以將數(shù)據(jù)寫入音頻文件代替。
- import audio from '@ohos.multimedia.audio';
- import fs from '@ohos.file.fs';
- const TAG = 'VoiceCallDemoForAudioRenderer';
- // 與使用AudioRenderer開發(fā)音頻播放功能過程相似,關(guān)鍵區(qū)別在于audioRendererInfo參數(shù)和音頻數(shù)據(jù)來源
- export default class VoiceCallDemoForAudioRenderer {
- private renderModel = undefined;
- private audioStreamInfo = {
- samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // 采樣率
- channels: audio.AudioChannel.CHANNEL_2, // 通道
- sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采樣格式
- encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 編碼格式
- }
- private audioRendererInfo = {
- // 需使用通話場景相應(yīng)的參數(shù)
- content: audio.ContentType.CONTENT_TYPE_SPEECH, // 音頻內(nèi)容類型:語音
- usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, // 音頻流使用類型:語音通信
- rendererFlags: 0 // 音頻渲染器標(biāo)志:默認(rèn)為0即可
- }
- private audioRendererOptions = {
- streamInfo: this.audioStreamInfo,
- rendererInfo: this.audioRendererInfo
- }
- // 初始化,創(chuàng)建實(shí)例,設(shè)置監(jiān)聽事件
- init() {
- audio.createAudioRenderer(this.audioRendererOptions, (err, renderer) => { // 創(chuàng)建AudioRenderer實(shí)例
- if (!err) {
- console.info(`${TAG}: creating AudioRenderer success`);
- this.renderModel = renderer;
- this.renderModel.on('stateChange', (state) => { // 設(shè)置監(jiān)聽事件,當(dāng)轉(zhuǎn)換到指定的狀態(tài)時(shí)觸發(fā)回調(diào)
- if (state == 1) {
- console.info('audio renderer state is: STATE_PREPARED');
- }
- if (state == 2) {
- console.info('audio renderer state is: STATE_RUNNING');
- }
- });
- this.renderModel.on('markReach', 1000, (position) => { // 訂閱markReach事件,當(dāng)渲染的幀數(shù)達(dá)到1000幀時(shí)觸發(fā)回調(diào)
- if (position == 1000) {
- console.info('ON Triggered successfully');
- }
- });
- } else {
- console.info(`${TAG}: creating AudioRenderer failed, error: ${err.message}`);
- }
- });
- }
- // 開始一次音頻渲染
- async start() {
- let stateGroup = [audio.AudioState.STATE_PREPARED, audio.AudioState.STATE_PAUSED, audio.AudioState.STATE_STOPPED];
- if (stateGroup.indexOf(this.renderModel.state) === -1) { // 當(dāng)且僅當(dāng)狀態(tài)為STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一時(shí)才能啟動(dòng)渲染
- console.error(TAG + 'start failed');
- return;
- }
- await this.renderModel.start(); // 啟動(dòng)渲染
- const bufferSize = await this.renderModel.getBufferSize();
- // 此處僅以讀取音頻文件的數(shù)據(jù)舉例,實(shí)際音頻通話開發(fā)中,需要讀取的是通話對端傳輸來的音頻數(shù)據(jù)
- let context = getContext(this);
- let path = context.filesDir;
- const filePath = path + '/voice_call_data.wav'; // 沙箱路徑,實(shí)際路徑為/data/storage/el2/base/haps/entry/files/voice_call_data.wav
- let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
- let stat = await fs.stat(filePath);
- let buf = new ArrayBuffer(bufferSize);
- let len = stat.size % bufferSize === 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1);
- for (let i = 0; i < len; i++) {
- let options = {
- offset: i * bufferSize,
- length: bufferSize
- };
- let readsize = await fs.read(file.fd, buf, options);
- // buf是要寫入緩沖區(qū)的音頻數(shù)據(jù),在調(diào)用AudioRenderer.write()方法前可以進(jìn)行音頻數(shù)據(jù)的預(yù)處理,實(shí)現(xiàn)個(gè)性化的音頻播放功能,AudioRenderer會(huì)讀出寫入緩沖區(qū)的音頻數(shù)據(jù)進(jìn)行渲染
- let writeSize = await new Promise((resolve, reject) => {
- this.renderModel.write(buf, (err, writeSize) => {
- if (err) {
- reject(err);
- } else {
- resolve(writeSize);
- }
- });
- });
- if (this.renderModel.state === audio.AudioState.STATE_RELEASED) { // 如果渲染器狀態(tài)為STATE_RELEASED,停止渲染
- fs.close(file);
- await this.renderModel.stop();
- }
- if (this.renderModel.state === audio.AudioState.STATE_RUNNING) {
- if (i === len - 1) { // 如果音頻文件已經(jīng)被讀取完,停止渲染
- fs.close(file);
- await this.renderModel.stop();
- }
- }
- }
- }
- // 暫停渲染
- async pause() {
- // 只有渲染器狀態(tài)為STATE_RUNNING的時(shí)候才能暫停
- if (this.renderModel.state !== audio.AudioState.STATE_RUNNING) {
- console.info('Renderer is not running');
- return;
- }
- await this.renderModel.pause(); // 暫停渲染
- if (this.renderModel.state === audio.AudioState.STATE_PAUSED) {
- console.info('Renderer is paused.');
- } else {
- console.error('Pausing renderer failed.');
- }
- }
- // 停止渲染
- async stop() {
- // 只有渲染器狀態(tài)為STATE_RUNNING或STATE_PAUSED的時(shí)候才可以停止
- if (this.renderModel.state !== audio.AudioState.STATE_RUNNING && this.renderModel.state !== audio.AudioState.STATE_PAUSED) {
- console.info('Renderer is not running or paused.');
- return;
- }
- await this.renderModel.stop(); // 停止渲染
- if (this.renderModel.state === audio.AudioState.STATE_STOPPED) {
- console.info('Renderer stopped.');
- } else {
- console.error('Stopping renderer failed.');
- }
- }
- // 銷毀實(shí)例,釋放資源
- async release() {
- // 渲染器狀態(tài)不是STATE_RELEASED狀態(tài),才能release
- if (this.renderModel.state === audio.AudioState.STATE_RELEASED) {
- console.info('Renderer already released');
- return;
- }
- await this.renderModel.release(); // 釋放資源
- if (this.renderModel.state === audio.AudioState.STATE_RELEASED) {
- console.info('Renderer released');
- } else {
- console.error('Renderer release failed.');
- }
- }
- }
- import audio from '@ohos.multimedia.audio';
- import fs from '@ohos.file.fs';
- const TAG = 'VoiceCallDemoForAudioCapturer';
- // 與使用AudioCapturer開發(fā)音頻錄制功能過程相似,關(guān)鍵區(qū)別在于audioCapturerInfo參數(shù)和音頻數(shù)據(jù)流向
- export default class VoiceCallDemoForAudioCapturer {
- private audioCapturer = undefined;
- private audioStreamInfo = {
- samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, // 采樣率
- channels: audio.AudioChannel.CHANNEL_1, // 通道
- sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 采樣格式
- encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 編碼格式
- }
- private audioCapturerInfo = {
- // 需使用通話場景相應(yīng)的參數(shù)
- source: audio.SourceType.SOURCE_TYPE_VOICE_COMMUNICATION, // 音源類型:語音通話
- capturerFlags: 0 // 音頻采集器標(biāo)志:默認(rèn)為0即可
- }
- private audioCapturerOptions = {
- streamInfo: this.audioStreamInfo,
- capturerInfo: this.audioCapturerInfo
- }
- // 初始化,創(chuàng)建實(shí)例,設(shè)置監(jiān)聽事件
- init() {
- audio.createAudioCapturer(this.audioCapturerOptions, (err, capturer) => { // 創(chuàng)建AudioCapturer實(shí)例
- if (err) {
- console.error(`Invoke createAudioCapturer failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info(`${TAG}: create AudioCapturer success`);
- this.audioCapturer = capturer;
- this.audioCapturer.on('markReach', 1000, (position) => { // 訂閱markReach事件,當(dāng)采集的幀數(shù)達(dá)到1000時(shí)觸發(fā)回調(diào)
- if (position === 1000) {
- console.info('ON Triggered successfully');
- }
- });
- this.audioCapturer.on('periodReach', 2000, (position) => { // 訂閱periodReach事件,當(dāng)采集的幀數(shù)達(dá)到2000時(shí)觸發(fā)回調(diào)
- if (position === 2000) {
- console.info('ON Triggered successfully');
- }
- });
- });
- }
- // 開始一次音頻采集
- async start() {
- let stateGroup = [audio.AudioState.STATE_PREPARED, audio.AudioState.STATE_PAUSED, audio.AudioState.STATE_STOPPED];
- if (stateGroup.indexOf(this.audioCapturer.state) === -1) { // 當(dāng)且僅當(dāng)狀態(tài)為STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一時(shí)才能啟動(dòng)采集
- console.error(`${TAG}: start failed`);
- return;
- }
- await this.audioCapturer.start(); // 啟動(dòng)采集
- // 此處僅以將音頻數(shù)據(jù)寫入文件舉例,實(shí)際音頻通話開發(fā)中,需要將本端采集的音頻數(shù)據(jù)編碼打包,通過網(wǎng)絡(luò)發(fā)送給通話對端
- let context = getContext(this);
- const path = context.filesDir + '/voice_call_data.wav'; // 采集到的音頻文件存儲路徑
- let file = fs.openSync(path, 0o2 | 0o100); // 如果文件不存在則創(chuàng)建文件
- let fd = file.fd;
- let numBuffersToCapture = 150; // 循環(huán)寫入150次
- let count = 0;
- while (numBuffersToCapture) {
- let bufferSize = await this.audioCapturer.getBufferSize();
- let buffer = await this.audioCapturer.read(bufferSize, true);
- let options = {
- offset: count * bufferSize,
- length: bufferSize
- };
- if (buffer === undefined) {
- console.error(`${TAG}: read buffer failed`);
- } else {
- let number = fs.writeSync(fd, buffer, options);
- console.info(`${TAG}: write date: ${number}`);
- }
- numBuffersToCapture--;
- count++;
- }
- }
- // 停止采集
- async stop() {
- // 只有采集器狀態(tài)為STATE_RUNNING或STATE_PAUSED的時(shí)候才可以停止
- if (this.audioCapturer.state !== audio.AudioState.STATE_RUNNING && this.audioCapturer.state !== audio.AudioState.STATE_PAUSED) {
- console.info('Capturer is not running or paused');
- return;
- }
- await this.audioCapturer.stop(); // 停止采集
- if (this.audioCapturer.state === audio.AudioState.STATE_STOPPED) {
- console.info('Capturer stopped');
- } else {
- console.error('Capturer stop failed');
- }
- }
- // 銷毀實(shí)例,釋放資源
- async release() {
- // 采集器狀態(tài)不是STATE_RELEASED或STATE_NEW狀態(tài),才能release
- if (this.audioCapturer.state === audio.AudioState.STATE_RELEASED || this.audioCapturer.state === audio.AudioState.STATE_NEW) {
- console.info('Capturer already released');
- return;
- }
- await this.audioCapturer.release(); // 釋放資源
- if (this.audioCapturer.state == audio.AudioState.STATE_RELEASED) {
- console.info('Capturer released');
- } else {
- console.error('Capturer release failed');
- }
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: