gpt4 book ai didi

android - 从后台恢复 Activity 后以编程方式播放/暂停 YoutubePlayer

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:21 24 4
gpt4 key购买 nike

我的应用程序有一个用于播放 Youtube 视频的 Activity A(使用 YoutubePlayerFragment)这个 Activity A 有一个自己的按钮 B 来控制播放。当 Activity 加载并正在播放视频时,我可以使用 B 按钮来控制播放。然后我按下主页按钮。当我从最近的任务菜单恢复 Activity A 时,按钮 B 不再起作用。我只能通过它自己的播放按钮(在 YoutubePlayerFragment 内)播放视频。目前我在调用 onInitializationSuccess 时存储 YoutubePlayer 实例:

private val onInitializedListener = object : YouTubePlayer.OnInitializedListener {
override fun onInitializationSuccess(provider: YouTubePlayer.Provider, youTubePlayer: YouTubePlayer, wasRestored: Boolean) {
Log.d(DEBUG_TAG, "YoutubePlayer - onInitializationSuccess")
if (!wasRestored) {
mYoutubePlayer = youTubePlayer
mYoutubePlayer!!.setPlayerStateChangeListener(playerStateChangeListener)
mYoutubePlayer!!.setPlaybackEventListener(mPlaybackEventListener)
mYoutubePlayer!!.setShowFullscreenButton(false)
mYoutubePlayer!!.loadVideo(mCurrentVideoId)
} else {
Log.d(DEBUG_TAG, "Restored from a previously saved state")
}
}

override fun onInitializationFailure(provider: YouTubePlayer.Provider, youTubeInitializationResult: YouTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError) {
youTubeInitializationResult.getErrorDialog(this@KActivityPlayVideo, 1).show()
} else {
Toast.makeText(this@KActivityPlayVideo,
"Failed to initialize video, please try again!",
Toast.LENGTH_LONG).show()
}
}
}

按钮 B 使用此 YoutubePlayer 实例根据请求调用播放/暂停。但是当 Activity A 进入后台并恢复时,这就不再起作用了。谁能建议我这里有什么问题?非常感谢。

最佳答案

我希望这对你有帮助。

private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {

@Override
public void onPlaying() {
// Called when playback starts, either due to user action or call to play().

}

@Override
public void onPaused() {
// Called when playback is paused, either due to user action or call to pause().

}

@Override
public void onStopped() {
// Called when playback stops for a reason other than being paused.

}

@Override
public void onBuffering(boolean b) {
// Called when buffering starts or ends.
}

@Override
public void onSeekTo(int i) {
// Called when a jump in playback position occurs, either
// due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
}
}

关于android - 从后台恢复 Activity 后以编程方式播放/暂停 YoutubePlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40935844/

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