openharmony 鸿蒙 using-toneplayer-for-playback-sys

2026-08-25 浏览 (1)

Using TonePlayer for Audio Playback (for System Applications Only)

TonePlayer9+ 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 and add the sine waves to create a sound. The sound can then be played by using the AudioRenderer, and the playback task can also be managed by using the AudioRenderer. 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.

Supported Tone Types

The table below lists the supported tone types. You can call load() with audio.ToneType.type as a parameter to load the tone resource of the specified type.

Tone TypeValueDescription
TONE_TYPE_DIAL_00DTMF tone of key 0.
TONE_TYPE_DIAL_11DTMF tone of key 1.
TONE_TYPE_DIAL_22DTMF tone of key 2.
TONE_TYPE_DIAL_33DTMF tone of key 3.
TONE_TYPE_DIAL_44DTMF tone of key 4.
TONE_TYPE_DIAL_55DTMF tone of key 5.
TONE_TYPE_DIAL_66DTMF tone of key 6.
TONE_TYPE_DIAL_77DTMF tone of key 7.
TONE_TYPE_DIAL_88DTMF tone of key 8.
TONE_TYPE_DIAL_99DTMF tone of key 9.
TONE_TYPE_DIAL_S10DTMF tone of the star key (*).
TONE_TYPE_DIAL_P11DTMF tone of the pound key (#).
TONE_TYPE_DIAL_A12DTMF tone of key A.
TONE_TYPE_DIAL_B13DTMF tone of key B.
TONE_TYPE_DIAL_C14DTMF tone of key C.
TONE_TYPE_DIAL_D15DTMF tone of key D.
TONE_TYPE_COMMON_SUPERVISORY_DIAL100Supervisory tone - dial tone.
TONE_TYPE_COMMON_SUPERVISORY_BUSY101Supervisory tone - busy.
TONE_TYPE_COMMON_SUPERVISORY_CONGESTION102Supervisory tone - congestion.
TONE_TYPE_COMMON_SUPERVISORY_RADIO_ACK103Supervisory tone - radio path acknowledgment.
TONE_TYPE_COMMON_SUPERVISORY_RADIO_NOT_AVAILABLE104Supervisory tone - radio path not available.
TONE_TYPE_COMMON_SUPERVISORY_CALL_WAITING106Supervisory tone - call waiting tone.
TONE_TYPE_COMMON_SUPERVISORY_RINGTONE107Supervisory tone - ringing tone.
TONE_TYPE_COMMON_SUPERVISORY_CALL_HOLDING18+108Call hold tone.
TONE_TYPE_COMMON_PROPRIETARY_BEEP200Proprietary tone - beep tone.
TONE_TYPE_COMMON_PROPRIETARY_ACK201Proprietary tone - ACK.
TONE_TYPE_COMMON_PROPRIETARY_PROMPT203Proprietary tone - PROMPT.
TONE_TYPE_COMMON_PROPRIETARY_DOUBLE_BEEP204Proprietary tone - double beep tone.

How to Develop

To implement audio playback with the TonePlayer, perform the following steps:

  1. Create a TonePlayer instance.

    import { audio } from '@kit.AudioKit';
    
    let audioRendererInfo: audio.AudioRendererInfo = {
      usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario.
      rendererFlags: 0 // AudioRenderer flag.
    };
    
    async function createTonePlayer() {
      let tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
    }
    
  2. Load the DTMF tone configuration of the specified type.

    async function load() {
      await tonePlayerPromise.load(audio.ToneType.TONE_TYPE_DIAL_0);
    }
    
  3. Start DTMF tone playing.

    async function start() {
      await tonePlayerPromise.start();
    }
    
  4. Stop the tone that is being played.

    async function stop() {
      await tonePlayerPromise.stop();
    }
    
  5. Release the resources associated with the TonePlayer instance.

    async function release() {
      await tonePlayerPromise.release();
    }
    

If the APIs are not called in the preceding sequence, the error code 6800301 NAPI_ERR_SYSTEM is returned.

Complete Sample Code

Refer to the following code to play the DTMF tone when the dial key on the keyboard is pressed.

To 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.

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let timerPro : number;
// Promise mode.
async function testTonePlayerPromise(type: audio.ToneType) {
  console.info('testTonePlayerPromise start');
  if (timerPro) clearTimeout(timerPro);
  let tonePlayerPromise: audio.TonePlayer;
  let audioRendererInfo: audio.AudioRendererInfo = {
    usage: audio.StreamUsage.STREAM_USAGE_DTMF, // Audio stream usage type: DTMF. Set this parameter based on the service scenario.
    rendererFlags: 0 // AudioRenderer flag.
  };
  timerPro = setTimeout(async () => {
    try {
      console.info('testTonePlayerPromise: createTonePlayer');
      // Create a DTMF player.  
      tonePlayerPromise = await audio.createTonePlayer(audioRendererInfo);
      console.info('testTonePlayerPromise: createTonePlayer-success');
      console.info(`testTonePlayerPromise: load type: ${type}`);
      // Load the tone configuration of the specified type.
      await tonePlayerPromise.load(type);
      console.info('testTonePlayerPromise: load-success');
      console.info(`testTonePlayerPromise: start type: ${type}`);
      // Start DTMF tone playing.
      await tonePlayerPromise.start();
      console.info('testTonePlayerPromise: start-success');
      console.info(`testTonePlayerPromise: stop type: ${type}`);
      setTimeout(async()=>{
        // Stop the tone that is being played.
        await tonePlayerPromise.stop();
        console.info('testTonePlayerPromise: stop-success');
        console.info(`testTonePlayerPromise: release type: ${type}`);
        // Release the resources associated with the TonePlayer instance.
        await tonePlayerPromise.release();
        console.info('testTonePlayerPromise: release-success');
      }, 30)
    } catch(err) {
      let error = err as BusinessError;
      console.error(`testTonePlayerPromise err : ${error}`);
    }
  }, 200)
};

async function testTonePlayer() {
   testTonePlayerPromise(audio.ToneType.TONE_TYPE_DIAL_0);
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 public-audio-spatialization-management

openharmony 鸿蒙 audio-session-management

openharmony 鸿蒙 using-opensl-es-for-playback

openharmony 鸿蒙 using-ohaudio-for-recording

openharmony 鸿蒙 audio-kit-intro

openharmony 鸿蒙 audio-workgroup

openharmony 鸿蒙 audio-collaborative-management-sys

openharmony 鸿蒙 audio-recording-concurrency

openharmony 鸿蒙 audio-recording-overview

openharmony 鸿蒙 audio-suite-manual-rendering

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/hqpFCgDi