• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using TonePlayer for Audio Playback (for System Applications Only)
2
3TonePlayer<sup>9+</sup> provides APIs for playing and managing Dual Tone Multi Frequency (DTMF) tones, such as dial tones, ringback tones, supervisory tones, and proprietary tones. The main task of the TonePlayer is to generate sine waves of different frequencies by using the built-in algorithm based on the [ToneType](../../reference/apis-audio-kit/js-apis-audio-sys.md#tonetype9) and add the sine waves to create a sound. The sound can then be played by using the [AudioRenderer](../../reference/apis-audio-kit/arkts-apis-audio-AudioRenderer.md), and the playback task can also be managed by using the [AudioRenderer](../../reference/apis-audio-kit/arkts-apis-audio-AudioRenderer.md). The full process includes loading the DTMF tone configuration, starting DTMF tone playing, stopping the playback, and releasing the resources associated with the TonePlayer object. For details about the APIs, see the [TonePlayer API Reference](../../reference/apis-audio-kit/js-apis-audio-sys.md#toneplayer9).
4
5## Supported Tone Types
6
7The table below lists the supported [tone types](../../reference/apis-audio-kit/js-apis-audio-sys.md#tonetype9). You can call **load()** with **audio.ToneType.*type*** as a parameter to load the tone resource of the specified type.
8
9| Tone Type| Value| Description|
10| -------- | -------- | -------- |
11| TONE_TYPE_DIAL_0 | 0 | DTMF tone of key 0.|
12| TONE_TYPE_DIAL_1 | 1 | DTMF tone of key 1.|
13| TONE_TYPE_DIAL_2 | 2 | DTMF tone of key 2.|
14| TONE_TYPE_DIAL_3 | 3 | DTMF tone of key 3.|
15| TONE_TYPE_DIAL_4 | 4 | DTMF tone of key 4.|
16| TONE_TYPE_DIAL_5 | 5 | DTMF tone of key 5.|
17| TONE_TYPE_DIAL_6 | 6 | DTMF tone of key 6.|
18| TONE_TYPE_DIAL_7 | 7 | DTMF tone of key 7.|
19| TONE_TYPE_DIAL_8 | 8 | DTMF tone of key 8.|
20| TONE_TYPE_DIAL_9 | 9 | DTMF tone of key 9.|
21| TONE_TYPE_DIAL_S | 10 | DTMF tone of the star key (*).|
22| TONE_TYPE_DIAL_P | 11 | DTMF tone of the pound key (#).|
23| TONE_TYPE_DIAL_A | 12 | DTMF tone of key A.|
24| TONE_TYPE_DIAL_B | 13 | DTMF tone of key B.|
25| TONE_TYPE_DIAL_C | 14 | DTMF tone of key C.|
26| TONE_TYPE_DIAL_D | 15 | DTMF tone of key D.|
27| TONE_TYPE_COMMON_SUPERVISORY_DIAL | 100 | Supervisory tone - dial tone.|
28| TONE_TYPE_COMMON_SUPERVISORY_BUSY | 101 | Supervisory tone - busy.|
29| TONE_TYPE_COMMON_SUPERVISORY_CONGESTION | 102 | Supervisory tone - congestion.|
30| TONE_TYPE_COMMON_SUPERVISORY_RADIO_ACK | 103 | Supervisory tone - radio path acknowledgment.|
31| TONE_TYPE_COMMON_SUPERVISORY_RADIO_NOT_AVAILABLE | 104 | Supervisory tone - radio path not available.|
32| TONE_TYPE_COMMON_SUPERVISORY_CALL_WAITING | 106 | Supervisory tone - call waiting tone.|
33| TONE_TYPE_COMMON_SUPERVISORY_RINGTONE | 107 | Supervisory tone - ringing tone.|
34| TONE_TYPE_COMMON_SUPERVISORY_CALL_HOLDING<sup>18+</sup> | 108 | Call hold tone.|
35| TONE_TYPE_COMMON_PROPRIETARY_BEEP | 200 | Proprietary tone - beep tone.|
36| TONE_TYPE_COMMON_PROPRIETARY_ACK | 201 | Proprietary tone - ACK.|
37| TONE_TYPE_COMMON_PROPRIETARY_PROMPT | 203 | Proprietary tone - PROMPT.|
38| TONE_TYPE_COMMON_PROPRIETARY_DOUBLE_BEEP | 204 | Proprietary tone - double beep tone.|
39
40## How to Develop
41
42To implement audio playback with the TonePlayer, perform the following steps:
43
441. Create a TonePlayer instance.
45
46   ```ts
47   import { audio } from '@kit.AudioKit';
48
49   let audioRendererInfo: audio.AudioRendererInfo = {
50     usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario.
51     rendererFlags: 0 // AudioRenderer flag.
52   };
53
54   async function createTonePlayer() {
55     let tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
56   }
57   ```
58
592. Load the DTMF tone configuration of the specified type.
60
61   ```ts
62   async function load() {
63     await tonePlayerPromise.load(audio.ToneType.TONE_TYPE_DIAL_0);
64   }
65   ```
66
673. Start DTMF tone playing.
68
69   ```ts
70   async function start() {
71     await tonePlayerPromise.start();
72   }
73   ```
74
754. Stop the tone that is being played.
76
77   ```ts
78   async function stop() {
79     await tonePlayerPromise.stop();
80   }
81   ```
82
835. Release the resources associated with the TonePlayer instance.
84
85   ```ts
86   async function release() {
87     await tonePlayerPromise.release();
88   }
89   ```
90
91If the APIs are not called in the preceding sequence, the error code **6800301 NAPI_ERR_SYSTEM** is returned.
92
93## Sample Code
94
95Refer to the following code to play the DTMF tone when the dial key on the keyboard is pressed.
96
97To prevent the UI thread from being blocked, most TonePlayer calls are asynchronous. Each API provides the callback and promise functions. The following examples use the promise functions. For more information, see [TonePlayer](../../reference/apis-audio-kit/js-apis-audio-sys.md#toneplayer9).
98
99```ts
100import { audio } from '@kit.AudioKit';
101import { BusinessError } from '@kit.BasicServicesKit';
102
103let timerPro : number;
104// Promise mode.
105async function testTonePlayerPromise(type: audio.ToneType) {
106  console.info('testTonePlayerPromise start');
107  if (timerPro) clearTimeout(timerPro);
108  let tonePlayerPromise: audio.TonePlayer;
109  let audioRendererInfo: audio.AudioRendererInfo = {
110    usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario.
111    rendererFlags: 0 // AudioRenderer flag.
112  };
113  timerPro = setTimeout(async () => {
114    try {
115      console.info('testTonePlayerPromise: createTonePlayer');
116      // Create a DTMF player.
117      tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
118      console.info('testTonePlayerPromise: createTonePlayer-success');
119      console.info(`testTonePlayerPromise: load type: ${type}`);
120      // Load the tone configuration of the specified type.
121      await tonePlayerPromise.load(type);
122      console.info('testTonePlayerPromise: load-success');
123      console.info(`testTonePlayerPromise: start type: ${type}`);
124      // Start DTMF tone playing.
125      await tonePlayerPromise.start();
126      console.info('testTonePlayerPromise: start-success');
127      console.info(`testTonePlayerPromise: stop type: ${type}`);
128      setTimeout(async()=>{
129        // Stop the tone that is being played.
130        await tonePlayerPromise.stop();
131        console.info('testTonePlayerPromise: stop-success');
132        console.info(`testTonePlayerPromise: release type: ${type}`);
133        // Release the resources associated with the TonePlayer instance.
134        await tonePlayerPromise.release();
135        console.info('testTonePlayerPromise: release-success');
136      }, 30)
137    } catch(err) {
138      let error = err as BusinessError;
139      console.error(`testTonePlayerPromise err : ${error}`);
140    }
141  }, 200)
142};
143
144async function testTonePlayer() {
145   testTonePlayerPromise(audio.ToneType.TONE_TYPE_DIAL_0);
146}
147```
148