Android消息提示音和振动管理类

Android消息提示音和振动管理类

主要参考ZXing中的声音管理类BeepManager中集成了声音播放和振动

参考的文章在末尾,有兴趣的可以也读一下

设置一些默认的全局参数

  • 设置音量
  • 设置振动时长

代码

/**
 * 类描述:消息提示音和振动管理类<br/>
 * 创建人:吴冬冬<br/>
 * 创建时间:2019/1/28 15:15<br/>
 */
class BeepAndVibrateManager constructor(context: Context) : MediaPlayer.OnErrorListener, Closeable {
    private var mContext: WeakReference<Context>? = null
    private var mMediaPlayer: MediaPlayer? = null

    private var mPlayBeep = false

    companion object {
        private const val BEEP_VOLUME = 0.1F;
        private const val VIBRATE_DURATION = 200L;
    }

    init {
        mContext = WeakReference(context)
        mMediaPlayer = null

        initBeep()
    }

    private fun initBeep() {
        val context = mContext?.get() ?: return

        mPlayBeep = shouldBeep(context)

        //设置声音基本设置
        if (mPlayBeep) {
            mMediaPlayer = buildMediaPlayer(context)
        }
    }

    /**
     * 播放和振动调用方法
     */
    @Synchronized
    @JvmOverloads
    fun playBeepSoundAndVibrate(isBeepSound: Boolean = true,
                                isVibrate: Boolean = true
    ) {
        val context = mContext?.get() ?: return
        if (isBeepSound) {
            if (shouldBeep(context)) {
                if (mMediaPlayer == null) {
                    mMediaPlayer = buildMediaPlayer(context)
                }
                mMediaPlayer!!.start()
            }
        }

        if (isVibrate) {
            val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
            vibrator.vibrate(VIBRATE_DURATION)
        }
    }

    private fun shouldBeep(context: Context): Boolean {
        val audioService = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        //如果手机不是普通模式就不让其有声音
        return audioService.ringerMode == AudioManager.RINGER_MODE_NORMAL
    }

    private fun buildMediaPlayer(context: Context): MediaPlayer {
        val mediaPlayer = MediaPlayer()
        //Z Xing 声音文件是放在raw下的
        //val file = context.resources.openRawResource(R.raw.beep)
        //我这里直接用系统默认声音提示
        mediaPlayer.setDataSource(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        mediaPlayer.setOnErrorListener(this)
        //设置该Activity中音量控制键控制的音频流
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)//媒体音量
        mediaPlayer.isLooping = false
        //左右声道声音大小
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME)
        mediaPlayer.prepare()
        return mediaPlayer
    }

    override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean {
        if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
            // we are finished, so put up an appropriate error toast if required and finish
            //context.finish()
        } else {
            // possibly media player error, so release and recreate
            close()
            initBeep()
        }
        return true
    }

    @Synchronized
    override fun close() {
        if (mMediaPlayer != null) {
            mMediaPlayer?.stop()
            mMediaPlayer?.release()
            mMediaPlayer = null
        }
    }
}

参考文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值