我想问一些有关将HTTP流传输到我的Android音频播放器的问题。在执行方面,它表现良好。当我按暂停并再次播放时,会出现以下错误:
目标网址为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);
}
}
答案 0 :(得分:1)
在您的代码中,initSetting
和config
方法应仅被调用一次。并且也不要将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
}