Android为拨号盘dialer定制声音DTMF Tones

DTMF :双音多频,电话系统中电话机与交换机之间的一种用户信令,通常用于发送被叫号码。

双音多频的拨号键盘是4*4的矩阵,每一行代表一个低频,每一列代表一个高频,每按一个键就发送一个高频和低频的正弦信号组合。比如'1'相当于697和1209赫兹(Hz)。交换机可以解码这些频率组合并确定所对应的按键。

现在我就想给自己的拨号键盘加上双音多频的声音,类似于固定电话拨号的声音。

页面就不贴出来了,类似于系统的拨号盘。

<span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> DialActivity extends Activity implements OnClickListener{
 
    <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">static</span> String TAG = <span style="color: rgb(0, 96, 128);">"【DialApp】"</span>;
    private static final int PLAY_TONE = 0x01;
    <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">static</span> final <span style="color: rgb(0, 0, 255);">int</span> DTMF_DURATION_MS = 120; <span style="color: rgb(0, 128, 0);">// 声音的播放时间</span>
    private Object mToneGeneratorLock = new Object(); // 监视器对象锁
    <span style="color: rgb(0, 0, 255);">private</span> ToneGenerator mToneGenerator;             <span style="color: rgb(0, 128, 0);">// 声音产生器</span>
    private static boolean mDTMFToneEnabled;         // 系统参数“按键操作音”标志位
    <span style="color: rgb(0, 128, 0);">// 存储DTMF Tones</span>
    private static final HashMap mToneMap = new HashMap();
 
    static {
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'1'</span>, ToneGenerator.TONE_DTMF_1);
        mToneMap.put('2', ToneGenerator.TONE_DTMF_2);
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'3'</span>, ToneGenerator.TONE_DTMF_3);
        mToneMap.put('4', ToneGenerator.TONE_DTMF_4);
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'5'</span>, ToneGenerator.TONE_DTMF_5);
        mToneMap.put('6', ToneGenerator.TONE_DTMF_6);
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'7'</span>, ToneGenerator.TONE_DTMF_7);
        mToneMap.put('8', ToneGenerator.TONE_DTMF_8);
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'9'</span>, ToneGenerator.TONE_DTMF_9);
        mToneMap.put('0', ToneGenerator.TONE_DTMF_0);
        mToneMap.put(<span style="color: rgb(0, 96, 128);">'#'</span>, ToneGenerator.TONE_DTMF_P);
        mToneMap.put('*', ToneGenerator.TONE_DTMF_S);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        <span style="color: rgb(0, 128, 0);">//按键声音播放设置及初始化</span>
        try {
            <span style="color: rgb(0, 128, 0);">// 获取系统参数“按键操作音”是否开启</span>
            mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
                    Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
            synchronized (mToneGeneratorLock) {
                <span style="color: rgb(0, 0, 255);">if</span> (mDTMFToneEnabled && mToneGenerator == <span style="color: rgb(0, 0, 255);">null</span>) {
                    mToneGenerator = new ToneGenerator(
                            AudioManager.STREAM_DTMF, 80); <span style="color: rgb(0, 128, 0);">// 设置声音的大小</span>
                    setVolumeControlStream(AudioManager.STREAM_DTMF);
                }
            }
        } <span style="color: rgb(0, 0, 255);">catch</span> (Exception e) {
            Log.d(TAG, e.getMessage());
            mDTMFToneEnabled = <span style="color: rgb(0, 0, 255);">false</span>;
            mToneGenerator = null;
        }
        /*
<span style="color: rgb(0, 128, 0);">         * 拨号按钮注册监听事件</span>
         * 
<span style="color: rgb(0, 128, 0);">         */</span>
    }
    @Override
    <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> onClick(View view) {
        int id = view.getId();   //点击按钮的id
        Integer currentTone;     <span style="color: rgb(0, 128, 0);">//获得当前的Tone</span>
        switch(id){
        <span style="color: rgb(0, 0, 255);">case</span> R.id.digit1:
            mToneMap.get("1");
            <span style="color: rgb(0, 0, 255);">break</span>;
        case R.id.digit2:
            mToneMap.get(<span style="color: rgb(0, 96, 128);">"2"</span>);
            break;
            .
            .
            .        
        }
        <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">null</span> != currentTone) {
            //第一种方式,采用Handler消息机制进行发声
            Message msg = mHandler.obtainMessage(PLAY_TONE, currentTone);
            mHandler.sendMessage(msg);
            <span style="color: rgb(0, 128, 0);">//第二种方式,直接调用playTone(int tone)方法,不知道二者有何区别?希望大家给点意见。</span>
//            playTone(currentTone);
        }
    }
    <span style="color: rgb(0, 128, 0);">/**</span>
     * 处理按键响应事件的消息
<span style="color: rgb(0, 128, 0);">     */</span>
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            <span style="color: rgb(0, 0, 255);">try</span> {
                switch (msg.what) {
                <span style="color: rgb(0, 0, 255);">case</span> PLAY_TONE:
                    Integer tone_id = (Integer) msg.obj;
                    <span style="color: rgb(0, 0, 255);">if</span> (tone_id != <span style="color: rgb(0, 0, 255);">null</span>) {
                        playTone(tone_id.intValue());
                    }
                    break;
                }
            } catch (Exception ex) {
                Log.d(TAG, ex.getMessage());
            }
        }
    };
 
    /**
<span style="color: rgb(0, 128, 0);">     * 播放按键声音</span>
     */
    <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> playTone(<span style="color: rgb(0, 0, 255);">int</span> tone) {
        if (!mDTMFToneEnabled) {
            <span style="color: rgb(0, 0, 255);">return</span>;
        }
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = audioManager.getRingerMode();
        <span style="color: rgb(0, 0, 255);">if</span> (ringerMode == AudioManager.RINGER_MODE_SILENT
                || ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            <span style="color: rgb(0, 128, 0);">// 静音或者震动时不发出声音</span>
            return;
        }
        synchronized (mToneGeneratorLock) {
            <span style="color: rgb(0, 0, 255);">if</span> (mToneGenerator == <span style="color: rgb(0, 0, 255);">null</span>) {
                Log.w(TAG, "playTone: mToneGenerator == null, tone: " + tone);
                <span style="color: rgb(0, 0, 255);">return</span>;
            }
            mToneGenerator.startTone(tone, DTMF_DURATION_MS);   <span style="color: rgb(0, 128, 0);">//发出声音</span>
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值