Android開發全局音量調整的實現方式詳解
引言
之前參與過一個項目,開發的是一個系統級別的軟件,安裝在定制的設備上,設備沒有控制音量的按鍵,因此軟件需要實現一個在任意頁面都能控制音量的功能。
實現方案是在所有頁面的頂部加上一個觸發音量控制彈窗的按鈕,用戶點擊該按鈕後顯示音量控制彈窗。
全局添加按鈕
參與項目時,已經出瞭第一版瞭,包含的頁面很多,因此一個個頁面去加肯定不合適。項目中所有Activity
都繼承瞭一個自定義的BaseActivity
,所以隻能在這個BaseActivity
中做文章。
Android中,每個Activity
都包含一個DecorView
,DecorView
內部包含一個FrameLayout
,可以通過android.R.id.content
來獲取,我們的佈局包含在這個FrameLayout
中。
因此如果需要在所有的頁面都添加View
,那麼在BaseActivity
中實現向android.R.id.content
對應的FrameLayout
添加View
的邏輯,然後所有的Activity
就都可以自動添加View
瞭。
實現代碼如下:
object DensityUtil { @JvmStatic fun dp2Px(dpValue: Int): Int { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue.toFloat(), Resources.getSystem().displayMetrics).toInt() } @JvmStatic fun px2Dp(pxValue: Int): Int { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, pxValue.toFloat(), Resources.getSystem().displayMetrics).toInt() } } // Base類 open class BaseActivity : AppCompatActivity() { override fun onResume() { super.onResume() // 在onResume中添加,確保在主佈局添加完成後添加,避免被遮擋。 initVolumeControllerView() } private fun initVolumeControllerView() { val controllerView = AppCompatImageView(this) controllerView.layoutParams = FrameLayout.LayoutParams(DensityUtil.dp2Px(80), DensityUtil.dp2Px(12)).apply { gravity = Gravity.START marginStart = DensityUtil.dp2Px(20) topMargin = DensityUtil.dp2Px(10) } controllerView.setImageResource(R.drawable.shape_vollume_controller) controllerView.setOnClickListener { runOnUiThread { Toast.makeText(this, "點擊瞭全局按鈕", Toast.LENGTH_SHORT).show() } } val rootView = findViewById<FrameLayout>(android.R.id.content) rootView.addView(controllerView) } }
效果如圖:
音量控制
AudioManager
類提供瞭控制音量的方法。
實現音量控制代碼如下:
class VolumeControllerDialog : DialogFragment() { private var binding: LayoutVolumeContollerDialogBinding? = null private var currentVolume = 0 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { dialog?.window?.run { setBackgroundDrawable(ContextCompat.getDrawable(requireContext(), android.R.color.transparent)) decorView.setBackgroundResource(android.R.color.transparent) val layoutParams = attributes layoutParams.width = DensityUtil.dp2Px(360) layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT layoutParams.gravity = Gravity.CENTER attributes = layoutParams } binding = DataBindingUtil.inflate(inflater, R.layout.layout_volume_contoller_dialog, container, false) return binding?.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val audioManager = requireContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) val step = 1 binding?.run { btnMute.text = getMuteButtonString(audioManager.isStreamMute(AudioManager.STREAM_MUSIC)) btnIncreaseVolume.setOnClickListener { // 增加音量 currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + step, AudioManager.FLAG_SHOW_UI or AudioManager.FLAG_PLAY_SOUND) btnMute.text = getMuteButtonString(audioManager.isStreamMute(AudioManager.STREAM_MUSIC)) } btnReduceVolume.setOnClickListener { //減少音量 currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, if (currentVolume - step < 0) 0 else currentVolume - step, AudioManager.FLAG_SHOW_UI or AudioManager.FLAG_PLAY_SOUND) btnMute.text = getMuteButtonString(audioManager.isStreamMute(AudioManager.STREAM_MUSIC)) } btnMute.setOnClickListener { // 靜音或取消靜音 val currentMute = audioManager.isStreamMute(AudioManager.STREAM_MUSIC) if (currentVolume == 0) { btnMute.text = getMuteButtonString(true) } else { btnMute.text = getMuteButtonString(!currentMute) } val setVolume = if (currentMute) { currentVolume } else { currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) 0 } audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, setVolume, AudioManager.FLAG_SHOW_UI or AudioManager.FLAG_PLAY_SOUND) } } } private fun getMuteButtonString(mute: Boolean): String { return if (mute) "UnMute" else "Mute" } } open class BaseActivity : AppCompatActivity() { override fun onResume() { super.onResume() // 在onResume中添加,確保在主佈局添加完成後添加,避免被遮擋。 initVolumeControllerView() } private fun initVolumeControllerView() { val controllerView = AppCompatImageView(this) controllerView.layoutParams = FrameLayout.LayoutParams(DensityUtil.dp2Px(80), DensityUtil.dp2Px(12)).apply { gravity = Gravity.START marginStart = DensityUtil.dp2Px(20) topMargin = DensityUtil.dp2Px(10) } controllerView.setImageResource(R.drawable.shape_vollume_controller) controllerView.setOnClickListener { VolumeControllerDialog().show(supportFragmentManager, null) } val rootView = findViewById<FrameLayout>(android.R.id.content) rootView.addView(controllerView) } }
效果如圖:
以上就是Android開發全局音量調整的實現方式詳解的詳細內容,更多關於Android 全局音量調整的資料請關註WalkonNet其它相關文章!