I am struggling to find a way to create a thread to monitor the playbackPosition in Exoplayer app.
In the following code, a simple task I need is to log the position parameter after every 1000ms by overriding the onIsPlayingChanged function. But I understand this happens only once and if the app state changes, however, to make this continuous, I think I should create a thread. Please let me know how can I achieve this. As you can see, the commented code is the one I tried but it is definitely not right.
private class PlaybackStateListener implements Player.EventListener{
@Override
public void onIsPlayingChanged(boolean isPlaying) {
//TODO: Add handler to poll the playback position continuously.
/* if(isPlaying){
HandlerCompat.
postDelayed(new Runnable() {
@Override
public void run() {
long positionParam = playbackPosition;
}
});
}*/
if (isPlaying) {
Log.d(TAG, "Playing position is " + playbackPosition);
} else {
// Not playing because playback is paused, ended, suppressed, or the player
// is buffering, stopped or failed. Check player.getPlaybackState,
// player.getPlayWhenReady, player.getPlaybackError and
// player.getPlaybackSuppressionReason for details.
}
}
Kotlin Coroutines way
private fun pollCurrentDuration() = flow {
while (player.currentPosition + DURATION_OFFSET <= player.duration) {
emit(player.currentPosition + DURATION_OFFSET)
delay(DEFAULT_DELAY_MS)
}
}.conflate()
Following piece of code can be used to resolve such issue using handler postDelayed method.
final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//My code
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
playbackPosition = player.getCurrentPosition();
send(playbackPosition); //Function to update position
handler.postDelayed(this, 1000); //1000ms frequency of updates.
}
}, 1000);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.