我的媒体播放器未调用setOnCompletionListener方法

时间:2019-04-14 02:39:17

标签: android kotlin

我正在使用Kotlin开发音乐播放器应用。我在 MainActivity 中定义了一个 mediaPlayer 对象,并在 SongPlayingFragments 中使用了该对象。 我遇到的问题是下一首歌曲会自动播放。 我试图找出问题所在,发现我的 onCompletionListener 无法正常工作,或者说没有被调用。 我本人无法解决问题,因此正在寻找任何解决方案或其他任何方式来自动更改我的歌曲。

    override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    CurrentSongHelper.isPlaying=true
    CurrentSongHelper.isShuffle= false
    CurrentSongHelper.isLoop = false
    var path: String ?= null
    var _songArtist: String ?= null
    var _songTitle: String ?= null
    var songId: Long = 0
    try {
        path = arguments?.getString("path")
        _songArtist = arguments?.getString("SongArtist")
        _songTitle = arguments?.getString("SongTitle")
        songId = arguments?.getInt("SongId")!!.toLong()
        currentPosition = arguments!!.getInt("SongPosition")
        fetchSongs = arguments?.getParcelableArrayList("SongData")

        CurrentSongHelper.songPath=path
        CurrentSongHelper.songArtist= _songArtist
        CurrentSongHelper.songTitle= _songTitle
        CurrentSongHelper.songId= songId
        CurrentSongHelper.currentPosition= currentPosition

        updateTextViews(CurrentSongHelper?.songTitle as String, CurrentSongHelper?.songArtist as String)
    }catch (e: Exception){
        e.printStackTrace()
    }
    //processInformation(mediaPlayer as MediaPlayer)
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
    try {
        mediaPlayer.setDataSource(myActivity as Context, Uri.parse(path))
        mediaPlayer.prepare()
        mediaPlayer.start()
    }catch (e: Exception){
        e.printStackTrace()
    }
    processInformation(mediaPlayer)
    if(CurrentSongHelper.isPlaying ){
        playPauseImageButton?.setBackgroundResource(R.drawable.pause_icon)
    }else{
        playPauseImageButton?.setBackgroundResource(R.drawable.play_icon )
    }

    mediaPlayer.setOnCompletionListener {
        Toast.makeText(context, "Changing Song", Toast.LENGTH_LONG).show()
        onSongComplete()
    }
    clickHandler()
    audioVisualization?.linkTo(mediaPlayer)
}

onSongComplete()函数应该更改我的歌曲。 Toast的“ Changing song”并没有消失,因此我认为无论出现什么问题,这都导致我的Listener无法被调用。

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

替换

mediaPlayer.setOnCompletionListener {
        Toast.makeText(context, "Changing Song", Toast.LENGTH_LONG).show()
        onSongComplete()
    }

使用

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(context, "Changing Song", Toast.LENGTH_LONG).show()
        onSongComplete()
            }
        });

答案 1 :(得分:0)

     videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash3));
        videoView.requestFocus();
        videoView.start();
        videoView.setOnCompletionListener(mp -> {
           //here code will come and it works.
        });

答案 2 :(得分:0)

在mediaplayer.start()之后调用完成侦听器代码

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
try {
    mediaPlayer.setDataSource(myActivity as Context, Uri.parse(path))
    mediaPlayer.prepare()
    mediaPlayer.start()

 mediaPlayer.setOnCompletionListener {
    Toast.makeText(context, "Changing Song", Toast.LENGTH_LONG).show()
    onSongComplete()
}

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