Android Kotlin:未触发MediaPlayer setOnCompletionListener

时间:2019-05-20 11:37:36

标签: kotlin android-mediaplayer

当我执行并播放媒体播放器的音频到最后时,不会触发setOnCompletionListener

您能告诉我我还需要设置什么吗?

 mediaPlayer = MediaPlayer()
            val audioBuilder = AudioAttributes.Builder()
            audioBuilder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            audioBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC)
            audioBuilder.setUsage(AudioAttributes.USAGE_MEDIA)
            showLoading()
            val attribute : AudioAttributes = audioBuilder.build()
            mediaPlayer!!.setAudioAttributes(attribute )
            mediaPlayer!!.setOnErrorListener { mp, what, extra ->
                mp.reset();
                Log.i("SYSTEM", "mp: ${mp}\nwhat: ${what}\nextra: $extra")
                false
            }


            mediaPlayer!!.setOnCompletionListener {
                Toast.makeText(this,"end",Toast.LENGTH_SHORT).show()
            }


            mediaPlayer!!.setOnPreparedListener {
                hideLoading()
                mediaPlayer!!.start()
            }

0 个答案:

没有答案