• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio Interruption Mode Development
2
3## Introduction
4The audio interruption mode is used to control the playback of multiple audio streams.
5
6Audio applications can set the audio interruption mode to independent or shared under **AudioRenderer**.
7
8In shared mode, multiple audio streams share one session ID. In independent mode, each audio stream has an independent session ID.
9
10**Asynchronous operation**: To prevent the UI thread from being blocked, most **AudioRenderer** calls are asynchronous. Each API provides the callback and promise functions. The following examples use the promise functions.
11
12## How to Develop
13
14For details about the APIs, see [AudioRenderer in Audio Management](../reference/apis/js-apis-audio.md#audiorenderer8).
15
161. Use **createAudioRenderer()** to create an **AudioRenderer** instance.
17
18   Set parameters of the **AudioRenderer** instance in **audioRendererOptions**.
19
20   This instance is used to render audio, control and obtain the rendering status, and register a callback for notification.
21
22```js
23   import audio from '@ohos.multimedia.audio';
24
25   var audioStreamInfo = {
26     samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
27     channels: audio.AudioChannel.CHANNEL_1,
28     sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
29  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
30   }
31
32   var audioRendererInfo = {
33     content: audio.ContentType.CONTENT_TYPE_SPEECH,
34     usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
35  rendererFlags: 1
36   }
37
38   var audioRendererOptions = {
39     streamInfo: audioStreamInfo,
40  rendererInfo: audioRendererInfo
41   }
42
43let audioRenderer = await audio.createAudioRenderer(audioRendererOptions);
44   ```
45
462. Set the audio interruption mode.
47
48   After the **AudioRenderer** instance is initialized, you can set the audio interruption mode.<br>
49
50   ```js
51    var mode_ = audio.InterruptMode.SHARE_MODE;
52    await this.audioRenderer.setInterruptMode(mode_).then(() => {
53      console.log('[JSAR] [SetInterruptMode] Setting: '+ (mode_ == 0? " share mode":"independent mode") + "success");
54    });
55   ```
56