我在我的应用程序中使用MediaPlayer作为服务。我实现了一个静音和非静音特性,但是当我在这两种状态之间切换时,我遇到了音量问题:
假设音乐正在以最大音量播放,当处于非静音状态时,您将音量降低到一半。然后你把声音静音,然后再解开它。虽然手机的媒体音量显示两次音量都是一样的,但我在静音后听到的声音明显比我静音前安静。
相反,在低音量播放时,当您在非静音状态下增加音量时,情况正好相反。在这种情况下,你解除静音后的音量听起来更响亮。
最后,当音量设置为0,然后非静音时,对音量的任何更改都不会对音频的响度产生影响。在这种情况下,音频保持静音,直到我按下静音,然后解除静音。
这让我相信,音量的响度,当音乐是非静音时,当你改变音量时,它对音频有一定的影响,但我不知道怎么做。
当用户解除静音时,我设置卷的方式是使用AudioManager和getStreamVolume进行Stream。
下面是代码:
主活性
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var mService: BackgroundSoundService? = null
var mIsBound: Boolean? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
//button to switch between muted and unmuted
binding.fab.setOnClickListener {
if (mService?.mute == true) {
val currentVolume = mService!!.getVolume()
mService?.mp?.setVolume(currentVolume, currentVolume)
mService?.setMuted(false)
} else if (mService?.mute == false) {
mService?.mp?.setVolume(0f, 0f)
mService?.setMuted(true)
}
}
}
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, iBinder: IBinder) {
val binder = iBinder as MyBinder
mService = binder.service
mIsBound = true
}
override fun onServiceDisconnected(arg0: ComponentName) {
mIsBound = false
}
}
private fun bindService() {
Intent(this, BackgroundSoundService::class.java).also { intent ->
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
}
}
private fun unbindService() {
Intent(this, BackgroundSoundService::class.java).also {
unbindService(serviceConnection)
}
}
override fun onStart() {
super.onStart()
bindService()
}
override fun onStop() {
super.onStop()
if (mIsBound == true) {
unbindService()
}
}
}MediaPlayer服务
class BackgroundSoundService : Service() {
var mute = false
private val mBinder: IBinder = MyBinder()
inner class MyBinder : Binder() {
val service: BackgroundSoundService
get() = this@BackgroundSoundService
}
var mp: MediaPlayer? = null
override fun onBind(intent: Intent): IBinder {
return mBinder
}
override fun onUnbind(intent: Intent?): Boolean {
mp?.stop()
mp?.release()
return false
}
override fun onCreate() {
super.onCreate()
val currentVolume = getVolume()
mp = MediaPlayer.create(this, R.raw.song)
mp?.isLooping = true
mp?.setVolume(currentVolume, currentVolume)
mp?.start()
}
fun setMuted(boolean: Boolean) {
mute = boolean
}
fun getVolume(): Float {
val audio = getSystemService(Context.AUDIO_SERVICE) as AudioManager
return audio.getStreamVolume(AudioManager.STREAM_MUSIC) / 15f
}
}任何帮助都很感激,
谢谢
发布于 2021-03-06 21:29:53
最后,我通过在服务中更改我的getVolume函数中的代码来使它工作起来:
fun getVolume(): Float {
val audio = getSystemService(Context.AUDIO_SERVICE) as AudioManager
val currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC)/ 15f
//just added the following line
return (ln(15 - currentVolume) / ln(15.0)).toFloat()
}老实说,我不明白为什么要这样做,所以如果你知道一种更有意义的方法,或者能向我解释这一点,我将不胜感激。
谢谢
https://stackoverflow.com/questions/66510361
复制相似问题