gpt4 book ai didi

android - setOnCompletionListener 方法未在我的媒体播放器中调用

转载 作者:太空狗 更新时间:2023-10-29 13:01:28 27 4
gpt4 key购买 nike

我正在使用 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 无法被调用。

有什么建议吗?

最佳答案

替换

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()
}
});

关于android - setOnCompletionListener 方法未在我的媒体播放器中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55671528/

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