• 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/js-apis-audio.md#tonetype9)s and add the sine waves to create a sound. The sound can then be played by using the [AudioRenderer](../reference/apis/js-apis-audio.md#audiorenderer8), and the playback task can also be managed by using the [AudioRenderer](../reference/apis/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/js-apis-audio.md#toneplayer9).
4
5
6## Supported Tone Types
7
8The table below lists the supported [ToneType](../reference/apis/js-apis-audio.md#tonetype9)s. You can call **load()** with **audio.ToneType.*type*** as a parameter to load the tone resource of the specified type.
9
10| Tone Type| Value| Description|
11| -------- | -------- | -------- |
12| TONE_TYPE_DIAL_0 | 0 | DTMF tone of key 0.|
13| TONE_TYPE_DIAL_1 | 1 | DTMF tone of key 1.|
14| TONE_TYPE_DIAL_2 | 2 | DTMF tone of key 2.|
15| TONE_TYPE_DIAL_3 | 3 | DTMF tone of key 3.|
16| TONE_TYPE_DIAL_4 | 4 | DTMF tone of key 4.|
17| TONE_TYPE_DIAL_5 | 5 | DTMF tone of key 5.|
18| TONE_TYPE_DIAL_6 | 6 | DTMF tone of key 6.|
19| TONE_TYPE_DIAL_7 | 7 | DTMF tone of key 7.|
20| TONE_TYPE_DIAL_8 | 8 | DTMF tone of key 8.|
21| TONE_TYPE_DIAL_9 | 9 | DTMF tone of key 9.|
22| TONE_TYPE_DIAL_S | 10 | DTMF tone of the star key (*).|
23| TONE_TYPE_DIAL_P | 11 | DTMF tone of the pound key (#).|
24| TONE_TYPE_DIAL_A | 12 | DTMF tone of key A.|
25| TONE_TYPE_DIAL_B | 13 | DTMF tone of key B.|
26| TONE_TYPE_DIAL_C | 14 | DTMF tone of key C.|
27| TONE_TYPE_DIAL_D | 15 | DTMF tone of key D.|
28| TONE_TYPE_COMMON_SUPERVISORY_DIAL | 100 | Supervisory tone - dial tone.|
29| TONE_TYPE_COMMON_SUPERVISORY_BUSY | 101 | Supervisory tone - busy.|
30| TONE_TYPE_COMMON_SUPERVISORY_CONGESTION | 102 | Supervisory tone - congestion.|
31| TONE_TYPE_COMMON_SUPERVISORY_RADIO_ACK | 103 | Supervisory tone - radio path acknowledgment.|
32| TONE_TYPE_COMMON_SUPERVISORY_RADIO_NOT_AVAILABLE | 104 | Supervisory tone - radio path not available.|
33| TONE_TYPE_COMMON_SUPERVISORY_CALL_WAITING | 106 | Supervisory tone - call waiting tone.|
34| TONE_TYPE_COMMON_SUPERVISORY_RINGTONE | 107 | Supervisory tone - ringing 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
41## How to Develop
42
43To implement audio playback with the TonePlayer, perform the following steps:
44
451. Create a **TonePlayer** instance.
46
47   ```ts
48   import audio from '@ohos.multimedia.audio';
49   let audioRendererInfo = {
50     content : audio.ContentType.CONTENT_TYPE_SONIFICATION,
51     usage : audio.StreamUsage.STREAM_USAGE_MEDIA,
52     rendererFlags : 0
53   };
54   tonePlayerPromise = audio.createTonePlayer(audioRendererInfo);
55   ```
56
572. Load the DTMF tone configuration of the specified type.
58
59   ```ts
60   tonePlayerPromise.load(audio.ToneType.TONE_TYPE_DIAL_0);
61   ```
62
633. Start DTMF tone playing.
64
65   ```ts
66   tonePlayerPromise.start();
67   ```
68
694. Stop the tone that is being played.
70
71   ```ts
72   tonePlayerPromise.stop();
73   ```
74
755. Release the resources associated with the **TonePlayer** instance.
76
77   ```ts
78   tonePlayerPromise.release();
79   ```
80
81If the APIs are not called in the preceding sequence, the error code **6800301 NAPI_ERR_SYSTEM** is returned.
82
83
84## Sample Code
85
86Refer to the following code to play the DTMF tone when the dial key on the keyboard is pressed.
87
88To 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/js-apis-audio.md#toneplayer9).
89
90
91```ts
92import audio from '@ohos.multimedia.audio';
93
94export class TonelayerDemo {
95  private timer : number;
96  private timerPro : number;
97  // Promise mode.
98  async testTonePlayerPromise(type) {
99    console.info('testTonePlayerPromise start');
100    if (this.timerPro) clearTimeout(this.timerPro);
101    let tonePlayerPromise;
102    let audioRendererInfo = {
103      content : audio.ContentType.CONTENT_TYPE_SONIFICATION,
104      usage : audio.StreamUsage.STREAM_USAGE_MEDIA,
105      rendererFlags : 0
106    };
107    this.timerPro = setTimeout(async () => {
108      try {
109        console.info('testTonePlayerPromise: createTonePlayer');
110        // Create a DTMF player.
111        tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
112        console.info('testTonePlayerPromise: createTonePlayer-success');
113        console.info(`testTonePlayerPromise: load type: ${type}`);
114        // Load the tone configuration of the specified type.
115        await tonePlayerPromise.load(type);
116        console.info('testTonePlayerPromise: load-success');
117        console.info(`testTonePlayerPromise: start type: ${type}`);
118        // Start DTMF tone playing.
119        await tonePlayerPromise.start();
120        console.info('testTonePlayerPromise: start-success');
121        console.info(`testTonePlayerPromise: stop type: ${type}`);
122        setTimeout(async()=>{
123          // Stop the tone that is being played.
124          await tonePlayerPromise.stop();
125          console.info('testTonePlayerPromise: stop-success');
126          console.info(`testTonePlayerPromise: release type: ${type}`);
127          // Release the resources associated with the TonePlayer instance.
128          await tonePlayerPromise.release();
129          console.info('testTonePlayerPromise: release-success');
130        }, 30)
131      } catch(err) {
132        console.error(`testTonePlayerPromise err : ${err}`);
133      }
134    }, 200)
135  };
136  async testTonePlayer() {
137    this.testTonePlayerPromise(audio.ToneType.TONE_TYPE_DIAL_0);
138  }
139}
140```
141