android学习日记:拨号按键声音自定义

司寇苗宣
2023-12-01

android中的拨号键盘音也是可以改变的,不过不同的是,按键音并不是存储在手机中的ogg格式的音频文件。他是通过C++代码中的频率设定来实现的。

具体的音频控制在frameworks/av/media/libmedia文件夹中的ToneGenerator.cpp中实现:

const ToneGenerator::ToneDescriptor ToneGenerator::sToneDescriptors[] = {
        { segments: {{ duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1336, 941, 0 }, 0, 0},
                     { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_0
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1209, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_1
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1336, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_2
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1477, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_3

我只截取了0-3号键盘的音频生成代码,其中的函数功能在ToneGenerator.h中有详细说明:

 - The array waveFreq[]:
    //         1 for static tone descriptors: contains the frequencies of all individual waves making the multi-tone.
    //         2 for active tone descritors: contains the indexes of the WaveGenerator objects in mWaveGens
    //        The number of sine waves varies from 1 to TONEGEN_MAX_WAVES.
    //        The first null value indicates that no more waves are needed.
    //    - The array segments[] is used to generate the tone pulses. A segment is a period of time
    //        during which the tone is ON or OFF.    Segments with even index (starting from 0)
    //        correspond to tone ON state and segments with odd index to OFF state.
    //        The data stored in segments[] is the duration of the corresponding period in ms.
    //        The first segment encountered with a 0 duration    indicates that no more segment follows.
    //    - loopCnt - Number of times to repeat a sequence of seqments after playing this
    //    - loopIndx - The segment index to go back and play is loopcnt > 0
    //    - repeatCnt indicates the number of times the sequence described by segments[] array must be repeated.
    //        When the tone generator encounters the first 0 duration segment, it will compare repeatCnt to mCurCount.
    //        If mCurCount > repeatCnt, the tone is stopped automatically. Otherwise, tone sequence will be
    //        restarted from segment repeatSegment.
    //    - repeatSegment number of the first repeated segment when repeatCnt is not null

本来想做成钢琴音的效果,但是对于频率波神马的无能为力。。。随意改动出来的效果实在难听。。。就放弃了。

 类似资料: