问题描述
我正在使用 Exoplayer 构建一个播客播放器。我已经设法使用 exoplayer 的 Playernotificationmanager
将其设置为在布局和通知栏中都具有控件。问题是,由于它与片段相关联,所以它在每次调用 onPause()
、onStop()
时都会停止播放,正如预期的那样。
所以我开始制作一个 ForegroundService
来运行 exoplayer 以便它继续播放,这非常成功,但是,布局中的控件不再起作用,只有通知栏控件起作用。
服务本身正在做它应该做的事情,但我不确定我需要做什么才能使布局控件也做出响应。
我基本上是在尝试构建一个标准的媒体播放器,但我对此有点不知所措。 exoplayer 的主要焦点似乎是视频,我理解与 mediaplayer 相比它有点沉重。我主要关注它,因为控件处理精美
有没有人对如何让 exoplayer 在具有通知栏控件和布局控件的服务中很好地播放有任何建议?
class podcastPlayFragment: Fragment(){
private val connection = object : ServiceConnection{
override fun onServiceConnected(name: ComponentName?,service: IBinder?) {
if (service is PlaypodcastService.PlaypodcastServiceBinder) {
playerControlView.player = service.getExoPlayer()
}
}
}
class PlaypodcastService: Service{
inner class PlaypodcastServiceBinder: Binder(){
fun getExoPlayer() = simpleExoPlayer
}
}
解决方法
在玩了一会儿之后,我意识到在 onBind()
中我没有返回一个 IBinder
对象。创建一个 IBinder 并在 onBind()
返回它解决了这个问题。
private val playPodcastIBinder: IBinder = PlayPodcastServiceBinder()
override fun onBind(intent: Intent?): IBinder? {
initializePlayer(context)
return playPodcastIBinder
}