Android 28 Kotlin 播放 http 流在暂停和播放时失败
Android 28 Kotlin play http streaming failed on Pause and Play again
我想问一些有关将 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);
}
}
根据您的代码,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
}
我想问一些有关将 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);
}
}
根据您的代码,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
}