ExoPlayer
ExoPlayer copied to clipboard
If I create Exoplayer instance in the thread which is not main thread , I can't get duration and current position from Exoplayer instance ( stuck in STATE_BUFFERING)
Hello team, I'm making a audio app using Exoplayer with waveForm library.
What I am making is when waveForm's progress has changed that Exoplayer's current position is changed by waveForm's progress.
I don't know my code is bad or not anyways, if both tasks are performed in the main thread, then audio is stuttering.
So I made Exoplayer instance at other thread which is newsingleThread in CoroutineContext.
I can pause() or play() Exoplayer but PlaybackState is always 2(means buffering) and can't get currentPosition() or duration()
How am I supposed to do in this case?
sorry, for my bad English.
I read docs but I can't understand this case.
val mediaDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private suspend fun createExoPlayer(audioFileName: String): ExoPlayer { return withContext(mediaDispatcher){ if(Looper.myLooper()==null){ Looper.prepare() } return@withContext ExoPlayer.Builder(context).build().apply {
setMediaItem(getMeidaItem(context, audioFileName))
playWhenReady=true
seekTo(0, 0L)
playbackParameters= PlaybackParameters(1f)
addListener(object: Player.Listener{
override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
reason: Int
) {
super.onPositionDiscontinuity(oldPosition, newPosition, reason)
launch(Dispatchers.Main){
mediaPosition.value=newPosition.positionMs
}
}
override fun onPlaybackStateChanged(playbackState: Int) {
when(playbackState){
ExoPlayer.STATE_IDLE -> Log.d("test","idle")
ExoPlayer.STATE_READY -> Log.d("test","ready")
ExoPlayer.STATE_BUFFERING -> Log.d("test","buffering")
ExoPlayer.STATE_ENDED -> Log.d("test","ended")
}
}
})
Log.d("test",playbackState.toString())
prepare()
Log.d("test",playbackState.toString())
}
}
}