我在我的应用程序的原始文件夹中添加了背景音乐" bg_music"并使用seekbar
创建一个活动,但我使用的seekbar
无法使用我的背景音乐,因为它不随音乐增加或减少。
这是我的代码:
class MusicActivity : Activity() {
private var imageButton6: ImageButton? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_music)
//Back button event
imageButton6 = findViewById(R.id.imageButton6) as ImageButton
imageButton6!!.setOnClickListener {
val i = Intent(applicationContext,
HomeActivity::class.java)
startActivity(i)
finish()
}
// call method VolumeControls()
VolumeControls()
}
private fun VolumeControls() {
try {
val txtView = findViewById(R.id.textView) as TextView // TextView
val seekBar2 = findViewById(R.id.seekBar) as SeekBar // SeekBar
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
seekBar2.max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
seekBar2.progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
// TextView
txtView.text = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC).toString()
seekBar2.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onStopTrackingTouch(arg0: SeekBar) {}
override fun onStartTrackingTouch(arg0: SeekBar) {}
override fun onProgressChanged(arg0: SeekBar, progress: Int, arg2: Boolean) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0)
txtView.text = progress.toString()
}
})
} catch (e: Exception) {
e.printStackTrace()
}
}