gpt4 book ai didi

Android 28 Kotlin 播放 http 流在暂停和播放时失败

转载 作者:行者123 更新时间:2023-11-29 23:18:56 30 4
gpt4 key购买 nike

我想问一些有关将 http 流式传输到我的 Android 音频播放器的问题。说到执行,它发挥得很好。当我按下暂停并再次播放时,出现以下错误:

目标 url 是 http://www.radioideal.net:8026/;stream/1

错误信息:

prepareAsync called in state 1, mPlayer(0x0)

您能告诉我管理 http mp3 音频流的正确顺序是什么吗?下面是我的工作。

代码:

class AudioActivity : AppCompatActivity() , EBookRetrieveRecordListener {

private var requestType = "audio"
private var mediaPlayer: MediaPlayer? = null
private var progressDialog: ProgressDialog? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_audio)
initSetting()
initView()
}

private fun initView() {
btn_audio_play.setOnClickListener({
if (mediaPlayer!!.isPlaying) pause() else play()
})
}

override fun onDestroy() {
super.onDestroy()
stop()

}

private fun play() {
config()
btn_audio_play!!.setImageResource(android.R.drawable.ic_media_pause)
}

private fun pause() {
mediaPlayer!!.pause()
mediaPlayer!!.reset();
btn_audio_play!!.setImageResource(android.R.drawable.ic_media_play)
}

private fun stop(){
mediaPlayer!!.stop()
mediaPlayer!!.release()
mediaPlayer = null
}


private fun config() {
try {
progressDialog = ProgressDialog(this)
progressDialog!!.setMessage(MESSAGE)
progressDialog!!.setCancelable(false)
progressDialog!!.show()
val audioBuilder = AudioAttributes.Builder()
audioBuilder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
audioBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
val attribute : AudioAttributes = audioBuilder.build()
mediaPlayer!!.setAudioAttributes(attribute )
mediaPlayer!!.isLooping = true
mediaPlayer!!.setOnPreparedListener {
it.start()
progressDialog!!.dismiss()
}

mediaPlayer!!.setOnErrorListener { mp, what, extra ->

Log.i(TAG, "mp: ${mp}\nwhat: ${what}\nextra: $extra")
progressDialog!!.dismiss()

false
}

mediaPlayer!!.setOnCompletionListener {
mediaPlayer!!.release()
}


mediaPlayer!!.setOnBufferingUpdateListener { mp, percent ->

mp.start()
}
mediaPlayer!!.prepareAsync()

} catch (e : Exception){
e.message
}
}

private fun initSetting() {
EBookRetrieveRecordManager.listener = this
//config()
val url = "http://www.radioideal.net:8026/;stream/1"
mediaPlayer = MediaPlayer()
mediaPlayer!!.setDataSource(url);

}
}

最佳答案

根据您的代码,initSettingconfig 方法应该只调用一次。也不要将 mediaPlayer!!.reset() 放在 play 方法中。

放在一起:

private var mediaPlayer: MediaPlayer? = null
private var progressDialog: ProgressDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_audio)
initView()
}

private fun initView() {
btn_audio_play.setOnClickListener {
mediaPlayer?.let {
if (it.isPlaying) pause() else resume()
} ?: run {
initSetting()
play()
}
}
}

private fun initSetting() {
val url = "http://www.radioideal.net:8026/;stream/1"
mediaPlayer = MediaPlayer()
mediaPlayer!!.setDataSource(url)
config()
}

private fun config() {
try {
progressDialog = ProgressDialog(this)
progressDialog!!.setMessage(MESSAGE)
progressDialog!!.setCancelable(false)
progressDialog!!.show()
val audioBuilder = AudioAttributes.Builder()
audioBuilder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
audioBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
val attribute: AudioAttributes = audioBuilder.build()
mediaPlayer!!.setAudioAttributes(attribute)
mediaPlayer!!.isLooping = true
mediaPlayer!!.setOnPreparedListener {
it.start()
progressDialog!!.dismiss()
}

mediaPlayer!!.setOnErrorListener { mp, what, extra ->
Log.i(TAG, "mp: ${mp}\nwhat: ${what}\nextra: $extra")
progressDialog!!.dismiss()
false
}

mediaPlayer!!.setOnCompletionListener {
mediaPlayer!!.release()
}

mediaPlayer!!.setOnBufferingUpdateListener { mp, percent ->
mp.start()
}
} catch (e: Exception) {
e.message
}
}

private fun play() {
mediaPlayer!!.prepareAsync()
btn_audio_play!!.setImageResource(android.R.drawable.ic_media_pause)
}

private fun pause() {
mediaPlayer!!.pause()
btn_audio_play!!.setImageResource(android.R.drawable.ic_media_play)
}

private fun resume() {
mediaPlayer!!.start()
btn_audio_play!!.setImageResource(android.R.drawable.ic_media_pause)
}

override fun onDestroy() {
super.onDestroy()
stop()
}

private fun stop() {
mediaPlayer!!.stop()
mediaPlayer!!.release()
mediaPlayer = null
}

关于Android 28 Kotlin 播放 http 流在暂停和播放时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807827/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com