1e41f4b71Sopenharmony_ci# Using TonePlayer for Audio Playback (for System Applications Only)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciTonePlayer<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/js-apis-audio.md#audiorenderer8), and the playback task can also be managed by using the [AudioRenderer](../../reference/apis-audio-kit/js-apis-audio.md#audiorenderer8). 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).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci## Supported Tone Types
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciThe table below lists the supported [ToneType](../../reference/apis-audio-kit/js-apis-audio-sys.md#tonetype9)s. You can call **load()** with **audio.ToneType.*type*** as a parameter to load the tone resource of the specified type.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci| Tone Type | Value | Description |
11e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
12e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_0 | 0 | DTMF tone of key 0. |
13e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_1 | 1 | DTMF tone of key 1. |
14e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_2 | 2 | DTMF tone of key 2. |
15e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_3 | 3 | DTMF tone of key 3. |
16e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_4 | 4 | DTMF tone of key 4. |
17e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_5 | 5 | DTMF tone of key 5. |
18e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_6 | 6 | DTMF tone of key 6. |
19e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_7 | 7 | DTMF tone of key 7. |
20e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_8 | 8 | DTMF tone of key 8. |
21e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_9 | 9 | DTMF tone of key 9. |
22e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_S | 10 | DTMF tone of the star key (*). |
23e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_P | 11 | DTMF tone of the pound key (#). |
24e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_A | 12 | DTMF tone of key A. |
25e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_B | 13 | DTMF tone of key B. |
26e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_C | 14 | DTMF tone of key C. |
27e41f4b71Sopenharmony_ci| TONE_TYPE_DIAL_D | 15 | DTMF tone of key D. |
28e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_DIAL | 100 | Supervisory tone - dial tone. |
29e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_BUSY | 101 | Supervisory tone - busy. |
30e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_CONGESTION | 102 | Supervisory tone - congestion. |
31e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_RADIO_ACK | 103 | Supervisory tone - radio path acknowledgment. |
32e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_RADIO_NOT_AVAILABLE | 104 | Supervisory tone - radio path not available. |
33e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_CALL_WAITING | 106 | Supervisory tone - call waiting tone. |
34e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_SUPERVISORY_RINGTONE | 107 | Supervisory tone - ringing tone. |
35e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_PROPRIETARY_BEEP | 200 | Proprietary tone - beep tone. |
36e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_PROPRIETARY_ACK | 201 | Proprietary tone - ACK. |
37e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_PROPRIETARY_PROMPT | 203 | Proprietary tone - PROMPT. |
38e41f4b71Sopenharmony_ci| TONE_TYPE_COMMON_PROPRIETARY_DOUBLE_BEEP | 204 | Proprietary tone - double beep tone. |
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci## How to Develop
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciTo implement audio playback with the TonePlayer, perform the following steps:
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci1. Create a **TonePlayer** instance.
46e41f4b71Sopenharmony_ci   
47e41f4b71Sopenharmony_ci   ```ts
48e41f4b71Sopenharmony_ci   import { audio } from '@kit.AudioKit';
49e41f4b71Sopenharmony_ci   
50e41f4b71Sopenharmony_ci   let audioRendererInfo: audio.AudioRendererInfo = {
51e41f4b71Sopenharmony_ci     usage : audio.StreamUsage.STREAM_USAGE_DTMF,
52e41f4b71Sopenharmony_ci     rendererFlags : 0
53e41f4b71Sopenharmony_ci   };
54e41f4b71Sopenharmony_ci   async function createTonePlayer() {
55e41f4b71Sopenharmony_ci     let tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
56e41f4b71Sopenharmony_ci   }
57e41f4b71Sopenharmony_ci   ```
58e41f4b71Sopenharmony_ci   
59e41f4b71Sopenharmony_ci2. Load the DTMF tone configuration of the specified type.
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci   ```ts
62e41f4b71Sopenharmony_ci   async function load() {
63e41f4b71Sopenharmony_ci       await tonePlayerPromise.load(audio.ToneType.TONE_TYPE_DIAL_0);
64e41f4b71Sopenharmony_ci   }
65e41f4b71Sopenharmony_ci   ```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci3. Start DTMF tone playing.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci   ```ts
70e41f4b71Sopenharmony_ci   async function start() {
71e41f4b71Sopenharmony_ci       await tonePlayerPromise.start();
72e41f4b71Sopenharmony_ci   }
73e41f4b71Sopenharmony_ci   ```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci4. Stop the tone that is being played.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci   ```ts
78e41f4b71Sopenharmony_ci   async function stop() {
79e41f4b71Sopenharmony_ci       await tonePlayerPromise.stop();
80e41f4b71Sopenharmony_ci   }
81e41f4b71Sopenharmony_ci   ```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci5. Release the resources associated with the **TonePlayer** instance.
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci   ```ts
86e41f4b71Sopenharmony_ci   async function release() {
87e41f4b71Sopenharmony_ci       await tonePlayerPromise.release();
88e41f4b71Sopenharmony_ci   }
89e41f4b71Sopenharmony_ci   ```
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ciIf the APIs are not called in the preceding sequence, the error code **6800301 NAPI_ERR_SYSTEM** is returned.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci## Sample Code
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ciRefer to the following code to play the DTMF tone when the dial key on the keyboard is pressed.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ciTo 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).
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci```ts
102e41f4b71Sopenharmony_ciimport { audio } from '@kit.AudioKit';
103e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_cilet timerPro : number;
106e41f4b71Sopenharmony_ci// Promise mode.
107e41f4b71Sopenharmony_ciasync function testTonePlayerPromise(type: audio.ToneType) {
108e41f4b71Sopenharmony_ci  console.info('testTonePlayerPromise start');
109e41f4b71Sopenharmony_ci  if (timerPro) clearTimeout(timerPro);
110e41f4b71Sopenharmony_ci  let tonePlayerPromise: audio.TonePlayer;
111e41f4b71Sopenharmony_ci  let audioRendererInfo: audio.AudioRendererInfo = {
112e41f4b71Sopenharmony_ci    usage : audio.StreamUsage.STREAM_USAGE_DTMF,
113e41f4b71Sopenharmony_ci    rendererFlags : 0
114e41f4b71Sopenharmony_ci  };
115e41f4b71Sopenharmony_ci  timerPro = setTimeout(async () => {
116e41f4b71Sopenharmony_ci    try {
117e41f4b71Sopenharmony_ci      console.info('testTonePlayerPromise: createTonePlayer');
118e41f4b71Sopenharmony_ci      // Create a DTMF player.     
119e41f4b71Sopenharmony_ci      tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
120e41f4b71Sopenharmony_ci      console.info('testTonePlayerPromise: createTonePlayer-success');
121e41f4b71Sopenharmony_ci      console.info(`testTonePlayerPromise: load type: ${type}`);
122e41f4b71Sopenharmony_ci      // Load the tone configuration of the specified type.
123e41f4b71Sopenharmony_ci      await tonePlayerPromise.load(type);
124e41f4b71Sopenharmony_ci      console.info('testTonePlayerPromise: load-success');
125e41f4b71Sopenharmony_ci      console.info(`testTonePlayerPromise: start type: ${type}`);
126e41f4b71Sopenharmony_ci      // Start DTMF tone playing.
127e41f4b71Sopenharmony_ci      await tonePlayerPromise.start();
128e41f4b71Sopenharmony_ci      console.info('testTonePlayerPromise: start-success');
129e41f4b71Sopenharmony_ci      console.info(`testTonePlayerPromise: stop type: ${type}`);
130e41f4b71Sopenharmony_ci      setTimeout(async()=>{
131e41f4b71Sopenharmony_ci        // Stop the tone that is being played.
132e41f4b71Sopenharmony_ci        await tonePlayerPromise.stop();
133e41f4b71Sopenharmony_ci        console.info('testTonePlayerPromise: stop-success');
134e41f4b71Sopenharmony_ci        console.info(`testTonePlayerPromise: release type: ${type}`);
135e41f4b71Sopenharmony_ci        // Release the resources associated with the TonePlayer instance.
136e41f4b71Sopenharmony_ci        await tonePlayerPromise.release();
137e41f4b71Sopenharmony_ci        console.info('testTonePlayerPromise: release-success');
138e41f4b71Sopenharmony_ci      }, 30)
139e41f4b71Sopenharmony_ci    } catch(err) {
140e41f4b71Sopenharmony_ci      let error = err as BusinessError;
141e41f4b71Sopenharmony_ci      console.error(`testTonePlayerPromise err : ${error}`);
142e41f4b71Sopenharmony_ci    }
143e41f4b71Sopenharmony_ci  }, 200)
144e41f4b71Sopenharmony_ci};
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ciasync function testTonePlayer() {
147e41f4b71Sopenharmony_ci   testTonePlayerPromise(audio.ToneType.TONE_TYPE_DIAL_0);
148e41f4b71Sopenharmony_ci}
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci```
151