录音的时候播放音乐，如何自动中断录音
==================

#### 问题现象

录音的时候打开音乐软件播放音乐，不会中断录音，使用[on('audioInterrupt')](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-audio-audiocapturer#onaudiointerrupt10)接口也无法监听到打断事件，怎么实现暂停录音？

#### 背景知识

[AudioCapturer](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/using-audiocapturer-for-recording)是音频采集器，用于录制PCM（Pulse Code Modulation）音频数据，适合有音频开发经验的开发者实现更灵活的录制功能。

[on('audioInterrupt')](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-audio-audiocapturer#onaudiointerrupt10)可监听音频中断事件（当音频焦点发生变化时触发）。

[AudioStreamManager](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/audio-playback-stream-management#读取或监听所有音频流的变化)可以读取或监听所有音频流的变化信息。

#### 解决方案

播放流（如音乐）与录制流（如录音）是相互独立的，焦点变化无法监听到。可通过[on('audioRendererChange')](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-audio-audiostreammanager#onaudiorendererchange9)监听音频输出流变化，并使用[isStreamActive](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-audio-audiostreammanager#isstreamactive20)判断音频流处于活跃状态时，主动暂停录音，关键示例代码如下：

let audioManager = audio.getAudioManager();
let audioStreamManager = audioManager.getStreamManager();
audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => {
  for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) {
    let AudioRendererChangeInfo = AudioRendererChangeInfoArray\[i\];
    console.info(\`RendererChange on is called for ${i}\`, JSON.stringify(AudioRendererChangeInfo.rendererInfo));
    try {
      let isStreamActive = audioStreamManager.isStreamActive(AudioRendererChangeInfo.rendererInfo.usage);
      console.info(\`RendererChange IsStreamActive: ${isStreamActive}.\`);
      if (isStreamActive) {
        console.info(\`RendererChange IsStreamActive stop capturer\`);
        stop();
      }
    } catch (err) {
      console.error(\`RendererChange. code: ${err.code}, message: ${err.message}\`);
    }
  }
});

全部示例代码如下：

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo as fs, WriteOptions } from '@kit.CoreFileKit';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
import { PromptAction } from '@kit.ArkUI';

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, _// 音源类型：Mic音频源。根据业务场景配置，参考SourceType。_
  capturerFlags: 0 _// 音频采集器标志。_
};
let audioCapturerOptions: audio.AudioCapturerOptions = {
  streamInfo: audioStreamInfo,
  capturerInfo: audioCapturerInfo
};
let file: fs.File;
let readDataCallback: Callback<ArrayBuffer>;
let promptAction: PromptAction;

@Entry
@Component
export struct CapturerStopDemo {
  private context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;

  aboutToAppear(): void {
    promptAction = this.getUIContext().getPromptAction();
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    atManager.requestPermissionsFromUser(this.context, \['ohos.permission.MICROPHONE'\]);
  }

  build() {
    Column({ space: 20 }) {
      Button('初始化').width('100%')
        .onClick(async () => {
          initArguments(this.context);
          init();
        });

      Button('开始录制').width('100%')
        .onClick(async () => {
          start();
        });

      Button('停止录制').width('100%')
        .onClick(async () => {
          stop();
        });

      Button('释放资源').width('100%')
        .onClick(async () => {
          release();
        });
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .padding(20)
    .height('100%')
    .width('100%');
  }
}

async function initArguments(context: common.UIAbilityContext) {
  let bufferSize: number = 0;
  let path = context.cacheDir;
  let filePath = path + '/StarWars10s-2C-48000-4SW.pcm';
  file = fs.openSync(filePath, fs.OpenMode.READ\_WRITE | fs.OpenMode.CREATE);
  readDataCallback = (buffer: ArrayBuffer) => {
    let options: WriteOptions = {
      offset: bufferSize,
      length: buffer.byteLength
    };
    fs.writeSync(file.fd, buffer, options);
    bufferSize += buffer.byteLength;
  };
}

_// 初始化，创建实例，设置监听事件。_
async 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(\`create AudioCapturer success\`);
    audioCapturer = capturer;
    if (audioCapturer !== undefined) {
      try {
        audioCapturer.on('readData', readDataCallback);
      } catch (error) {
        console.error(\`create AudioCapturer error\`);
      }
    }
    promptAction.showToast({
      message: 'init success'
    });
  });

  let audioManager = audio.getAudioManager();
  let audioStreamManager = audioManager.getStreamManager();
  audioStreamManager.on('audioRendererChange', (AudioRendererChangeInfoArray: audio.AudioRendererChangeInfoArray) => {
    for (let i = 0; i < AudioRendererChangeInfoArray.length; i++) {
      let AudioRendererChangeInfo = AudioRendererChangeInfoArray\[i\];
      console.info(\`RendererChange on is called for ${i}\`, JSON.stringify(AudioRendererChangeInfo.rendererInfo));
      try {
        let isStreamActive = audioStreamManager.isStreamActive(AudioRendererChangeInfo.rendererInfo.usage);
        console.info(\`RendererChange IsStreamActive: ${isStreamActive}.\`);
        if (isStreamActive) {
          console.info(\`RendererChange IsStreamActive stop capturer\`);
          stop();
        }
      } catch (err) {
        console.error(\`RendererChange. code: ${err.code}, message: ${err.message}\`);
      }
    }
  });
}

_// 开始一次音频采集。_
async function start() {
  if (audioCapturer !== undefined) {
    let stateGroup = \[audio.AudioState.STATE\_PREPARED, audio.AudioState.STATE\_PAUSED, audio.AudioState.STATE\_STOPPED\];
    if (stateGroup.indexOf(audioCapturer.state.valueOf()) ===
      -1) { _// 当且仅当状态为STATE\_PREPARED、STATE\_PAUSED和STATE\_STOPPED之一时才能启动采集。_
      console.error(\`start failed\`);
      return;
    }

    _// 启动采集。_
    audioCapturer.start((err: BusinessError) => {
      if (err) {
        console.error('Capturer start failed.');
      } else {
        console.info('Capturer start success.');
        promptAction.showToast({
          message: 'start success'
        });
      }
    });
  }
}

_// 停止采集。_
async function stop() {
  if (audioCapturer !== undefined) {
    _// 只有采集器状态为STATE\_RUNNING或STATE\_PAUSED的时候才可以停止。_
    if (audioCapturer.state.valueOf() !== audio.AudioState.STATE\_RUNNING &&
      audioCapturer.state.valueOf() !== audio.AudioState.STATE\_PAUSED) {
      console.info('Capturer is not running or paused');
      return;
    }

   _ // 停止采集。_
    audioCapturer.stop((err: BusinessError) => {
      if (err) {
        console.error('Capturer stop failed.');
      } else {
        console.info('Capturer stop success.');
        promptAction.showToast({
          message: 'stop success'
        });
      }
    });
  }
}

_// 销毁实例，释放资源。_
async function release() {
  if (audioCapturer !== undefined) {
   _ // 采集器状态不是STATE\_RELEASED或STATE\_NEW状态，才能release。_
    if (audioCapturer.state.valueOf() === audio.AudioState.STATE\_RELEASED ||
      audioCapturer.state.valueOf() === audio.AudioState.STATE\_NEW) {
      console.info('Capturer already released');
      return;
    }
  _  // 释放资源。_
    audioCapturer.release((err: BusinessError) => {
      if (err) {
        console.error('Capturer release failed.');
      } else {
        fs.closeSync(file);
        console.info('Capturer release success.');
        promptAction.showToast({
          message: 'release success'
        });
      }
    });
  }
}