• Android 使用 ToneGenerator 实现按键提示音


    外部链接

    DTMF原理
    处理音频输出的变化
    ToneGenerator API

    简单效果

    5_1698069103

    功能简单实现

    工具类

    package com.xg.practise.util
    
    import android.app.Activity
    import android.content.Context
    import android.media.AudioManager
    import android.media.ToneGenerator
    import android.provider.Settings
    
    //Tone 相关
    const val DTMF_DURATION_MS = 120 // 声音的播放时间,毫秒
    
    class ToneHelper() {
        // 监视器对象锁
        private val mToneGeneratorLock = Any()
    
        // 声音产生器
        private var mToneGenerator
                : ToneGenerator? = null
    
        // 系统参数“按键操作音”标志位
        private var mDTMFToneEnabled = false
    
        fun initToneGenerator(context: Context) {
            runOnBackgroundThread(Runnable {
                //按键声音播放设置及初始化
                try {
                    // 获取系统参数“按键操作音”是否开启
                    mDTMFToneEnabled = Settings.System.getInt(
                        context.contentResolver,
                        Settings.System.DTMF_TONE_WHEN_DIALING, 1
                    ) == 1
                    synchronized(mToneGeneratorLock) {
                        if (mDTMFToneEnabled && mToneGenerator == null) {
                            mToneGenerator = ToneGenerator(
                                AudioManager.STREAM_DTMF, 100
                            ) // 设置声音的大小
                            if (context is Activity) {
                                context.volumeControlStream = AudioManager.STREAM_DTMF
                            }
                        }
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    mDTMFToneEnabled = false
                    mToneGenerator = null
                }
            })
        }
    
        fun playTone(context: Context, dtmf: Int) {
            if (dtmf < 0) {
                return
            }
            if (!mDTMFToneEnabled) {
                return
            }
            val audioManager =
                context.applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
            val ringerMode = audioManager.ringerMode
            if ((ringerMode == AudioManager.RINGER_MODE_SILENT
                        || ringerMode == AudioManager.RINGER_MODE_VIBRATE)
            ) {
                // 静音或者震动时不发出声音
                return
            }
            synchronized(mToneGeneratorLock) {
                mToneGenerator?.startTone(dtmf, DTMF_DURATION_MS) //发出声音
            }
        }
    
        fun release() {
            try {
                mDTMFToneEnabled = false
                mToneGenerator?.stopTone()
                mToneGenerator?.release()
                mToneGenerator = null
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    线程池工具

    package com.xg.practise.util
    
    import java.util.concurrent.Executors
    import java.util.concurrent.ScheduledExecutorService
    import java.util.concurrent.TimeUnit
    
    val executor: ScheduledExecutorService = Executors.newScheduledThreadPool(10)
    
    fun runOnBackgroundThread(runnable: Runnable?) {
        executor.execute(runnable)
    }
    
    fun runOnBackgroundThread(runnable: Runnable?, delay: Long) {
        executor.schedule(runnable, delay, TimeUnit.MILLISECONDS)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    主页面简单实现

    package com.xg.practise
    
    import android.media.ToneGenerator
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.TextView
    import androidx.appcompat.app.AppCompatActivity
    import androidx.databinding.DataBindingUtil
    import androidx.recyclerview.widget.GridLayoutManager
    import androidx.recyclerview.widget.RecyclerView
    import com.xg.practise.databinding.ActivityToneBinding
    import com.xg.practise.util.ToneHelper
    
    class ToneActivity : AppCompatActivity() {
        private var mToneHelper
                : ToneHelper? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            var dataBinding =
                DataBindingUtil.setContentView(this, R.layout.activity_tone);
            setContentView(dataBinding.root)
    
            mToneHelper = ToneHelper()
            mToneHelper?.initToneGenerator(this@ToneActivity)
    
            var array = arrayOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#")
            dataBinding.recycler.layoutManager = GridLayoutManager(this@ToneActivity, 3)
            dataBinding.recycler.adapter = object : RecyclerView.Adapter() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                    return MyViewHolder(
                        LayoutInflater.from(parent.context)
                            .inflate(android.R.layout.simple_list_item_1, parent, false)
                    )
                }
    
                override fun getItemCount(): Int {
                    return array.size
                }
    
                override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                    holder.text.text = array[position]
                    holder.text.setOnClickListener {
                        mToneHelper?.playTone(holder.text.context,convertToneDtmf(array[position]))
                    }
                }
    
            }
        }
    
        fun convertToneDtmf(key: String?): Int {
            return when (key) {
                "0" -> ToneGenerator.TONE_DTMF_0
                "1" -> ToneGenerator.TONE_DTMF_1
                "2" -> ToneGenerator.TONE_DTMF_2
                "3" -> ToneGenerator.TONE_DTMF_3
                "4" -> ToneGenerator.TONE_DTMF_4
                "5" -> ToneGenerator.TONE_DTMF_5
                "6" -> ToneGenerator.TONE_DTMF_6
                "7" -> ToneGenerator.TONE_DTMF_7
                "8" -> ToneGenerator.TONE_DTMF_8
                "9" -> ToneGenerator.TONE_DTMF_9
                "*" -> ToneGenerator.TONE_DTMF_S
                "#" -> ToneGenerator.TONE_DTMF_P
                else -> -1
            }
        }
    
        override fun onDestroy() {
            super.onDestroy()
            mToneHelper?.release()
        }
    
        class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val text: TextView = itemView.findViewById(android.R.id.text1)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    零代码工具拖拽流程图
    Mac OS下应用Python+Selenium实现web自动化测试
    复数的三角形式与指数形式
    【C语言】手把手带你拿指针疑难杂症
    【.net core】yisha框架imageupload组件多图上传修改
    论文阅读-ATLAS: A Sequence-based Learning Approach for Attack Investigation
    Leetcode 698. 划分为k个相等的子集
    用DIV+CSS技术设计的网上书城网页与实现制作(大一Web课程设计)
    嵌入式驱动初级-阻塞与非阻塞
    notification控件 通知栏
  • 原文地址:https://blog.csdn.net/hello_12413/article/details/133999751