如何在发布后重新初始化ExoPlayer?

问题描述 投票:0回答:1

我有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按钮后播放其他视频。所以在ExoPlayer方法调用之后重新初始化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)
android video kotlin exoplayer exoplayer2.x
1个回答
0
投票

根据您的要求,无需使用发布方法。如果您使用此方法,则应重新初始化exoplayer。

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

© www.soinside.com 2019 - 2024. All rights reserved.