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/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). 4 5## Supported Tone Types 6 7The 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. 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_PROPRIETARY_BEEP | 200 | Proprietary tone - beep tone.| 35| TONE_TYPE_COMMON_PROPRIETARY_ACK | 201 | Proprietary tone - ACK.| 36| TONE_TYPE_COMMON_PROPRIETARY_PROMPT | 203 | Proprietary tone - PROMPT.| 37| TONE_TYPE_COMMON_PROPRIETARY_DOUBLE_BEEP | 204 | Proprietary tone - double beep tone.| 38 39## How to Develop 40 41To implement audio playback with the TonePlayer, perform the following steps: 42 431. Create a **TonePlayer** instance. 44 45 ```ts 46 import { audio } from '@kit.AudioKit'; 47 48 let audioRendererInfo: audio.AudioRendererInfo = { 49 usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario. 50 rendererFlags: 0 // AudioRenderer flag. 51 }; 52 53 async function createTonePlayer() { 54 let tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo); 55 } 56 ``` 57 582. Load the DTMF tone configuration of the specified type. 59 60 ```ts 61 async function load() { 62 await tonePlayerPromise.load(audio.ToneType.TONE_TYPE_DIAL_0); 63 } 64 ``` 65 663. Start DTMF tone playing. 67 68 ```ts 69 async function start() { 70 await tonePlayerPromise.start(); 71 } 72 ``` 73 744. Stop the tone that is being played. 75 76 ```ts 77 async function stop() { 78 await tonePlayerPromise.stop(); 79 } 80 ``` 81 825. Release the resources associated with the **TonePlayer** instance. 83 84 ```ts 85 async function release() { 86 await tonePlayerPromise.release(); 87 } 88 ``` 89 90If the APIs are not called in the preceding sequence, the error code **6800301 NAPI_ERR_SYSTEM** is returned. 91 92## Sample Code 93 94Refer to the following code to play the DTMF tone when the dial key on the keyboard is pressed. 95 96To 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). 97 98```ts 99import { audio } from '@kit.AudioKit'; 100import { BusinessError } from '@kit.BasicServicesKit'; 101 102let timerPro : number; 103// Promise mode. 104async function testTonePlayerPromise(type: audio.ToneType) { 105 console.info('testTonePlayerPromise start'); 106 if (timerPro) clearTimeout(timerPro); 107 let tonePlayerPromise: audio.TonePlayer; 108 let audioRendererInfo: audio.AudioRendererInfo = { 109 usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario. 110 rendererFlags: 0 // AudioRenderer flag. 111 }; 112 timerPro = setTimeout(async () => { 113 try { 114 console.info('testTonePlayerPromise: createTonePlayer'); 115 // Create a DTMF player. 116 tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo); 117 console.info('testTonePlayerPromise: createTonePlayer-success'); 118 console.info(`testTonePlayerPromise: load type: ${type}`); 119 // Load the tone configuration of the specified type. 120 await tonePlayerPromise.load(type); 121 console.info('testTonePlayerPromise: load-success'); 122 console.info(`testTonePlayerPromise: start type: ${type}`); 123 // Start DTMF tone playing. 124 await tonePlayerPromise.start(); 125 console.info('testTonePlayerPromise: start-success'); 126 console.info(`testTonePlayerPromise: stop type: ${type}`); 127 setTimeout(async()=>{ 128 // Stop the tone that is being played. 129 await tonePlayerPromise.stop(); 130 console.info('testTonePlayerPromise: stop-success'); 131 console.info(`testTonePlayerPromise: release type: ${type}`); 132 // Release the resources associated with the TonePlayer instance. 133 await tonePlayerPromise.release(); 134 console.info('testTonePlayerPromise: release-success'); 135 }, 30) 136 } catch(err) { 137 let error = err as BusinessError; 138 console.error(`testTonePlayerPromise err : ${error}`); 139 } 140 }, 200) 141}; 142 143async function testTonePlayer() { 144 testTonePlayerPromise(audio.ToneType.TONE_TYPE_DIAL_0); 145} 146``` 147