如何在释放后重新定位系工



我有ExoPlayerWrapper类,它是ExoPlayer的包装器,并由 dagger 注射单例。ExoPlayerWrapper类中的init块以以下方式外观:

@Singleton
class ExoPlayerWrapper @Inject constructor(
    context: Context,
    userAgentInfo: UserAgentInfo
) {
    private val exoPlayer: ExoPlayer
    private val httpDataSource: HttpDataSource
    private val mediaSourceFactory: ExtractorMediaSource.Factory
    override val exoPlayerInstance: ExoPlayer
        get() = exoPlayer
   init {
        ...
        exoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector)
        httpDataSource = CustomHttpDataSource(userAgentInfo.userAgent, null)
        mediaSourceFactory = ExtractorMediaSource.Factory { httpDataSource }
        state = Player.STATE_IDLE
    }
   override fun playFromUrl(uri: Uri, headers: Map<String, String>) {
        reset()
        ...
        val mediaSource = mediaSourceFactory.createMediaSource(uri)
        exoPlayer.prepare(mediaSource)
        exoPlayer.playWhenReady = true
    }
    override fun pause() {
        exoPlayer.playWhenReady = false
    }
    override fun reset() {
       stop()
       state = Player.STATE_IDLE
    }
    override fun stop() {
       exoPlayer.stop()
       exoPlayer.seekTo(0)
    }
    override fun seekTo(position: Long) {
       exoPlayer.seekTo(position)
    }
    ...
    override fun release() {
        exoPlayer.release()
        state = Player.STATE_RELEASED
    }

onCreate() Activity的方法中,我有以下代码:

public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_video_player)
    ...
    if (savedInstanceState != null) {
        initialized = savedInstanceState.getBoolean(INITIALIZED)
    } 
    if (!initialized) {
        val uri = Uri.parse(attachedVideo?.uri)
        player.playFromUrl(uri, sessionStore.sessionHeaders)
        videoView.requestFocus()
        initialized = true
    }
    videoView.player = player.exoPlayerInstance
    backBtn.setOnClickListener{ _ ->
        player.release()
        finish()
    }
}

但是在这种情况下,我只能在单击backBtn按钮后才播放一个视频,此后其他视频才播放,直到我关闭应用程序并再次重新打开应用程序。如果我以以下方式更改backBtn's OnClickListener

backBtn.setOnClickListener{ _ ->
    player.pause()
    finish()
}

都可以正常工作,在单击backBtn按钮后播放其他视频。因此,在release()方法调用后重新定位ExoPlayer似乎是问题。以及在发布后如何正确重新定位系政区?

upd release()调用和尝试再次打开视频后,我在LogCat中看到以下错误:

java.lang.IllegalStateException: Handler (android.os.Handler) {81fa8ef} sending message to a Handler on a dead thread
    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:545)
    at android.os.Handler.enqueueMessage(Handler.java:661)
    at android.os.Handler.sendMessageAtTime(Handler.java:630)
    at android.os.Handler.sendMessageDelayed(Handler.java:600)
    at android.os.Handler.sendMessage(Handler.java:537)
    at android.os.Message.sendToTarget(Message.java:418)
    at com.google.android.exoplayer2.ExoPlayerImplInternal.stop(ExoPlayerImplInternal.java:207)
    at com.google.android.exoplayer2.ExoPlayerImpl.stop(ExoPlayerImpl.java:357)
    at com.google.android.exoplayer2.SimpleExoPlayer.stop(SimpleExoPlayer.java:777)
    at com.google.android.exoplayer2.SimpleExoPlayer.stop(SimpleExoPlayer.java:772)
    at package.ExoPlayerWrapper.stop(ExoPlayerWrapper.kt:xx)
    at package.ExoPlayerWrapper.reset(ExoPlayerWrapper.kt:xx)
    at package.ExoPlayerWrapper.playFromUrl(ExoPlayerWrapper.kt:xx)
    at package.VideoPlayerActivity.onCreate(VideoPlayerActivity.kt:xx)

问题源于尝试使用已经发布资源的播放器。因为:

exoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector)

ExoPlayerWrapper初始化期间,只能被调用一次。如果您的活动没有重新启动,则ExoPlayerWrapper的实例与已发布的播放器一起粘贴。

每当您播放视频时,可以创建ExoPlayerWrapper的新实例,或者在ExoPlayerWrapper中创建播放器初始化方法,您可以在播放视频之前拨打它。

为您的需求无需使用释放方法。如果您使用此方法,则应再次重新初次化epoplayer。

您可以使用stop()方法来停止系列。

相关内容

  • 没有找到相关文章

最新更新