I am having a very weird problem with hooking up a Seekbar to a MediaPlayer with mkv files with vorbis encoding. This is a supported format according to https://developer.android.com/guide/topics/media/media-formats
When the MediaPlayer is updated via a SeekBar.OnSeekBarChangeListener's onStopTrackingTouch method, the MediaPlayer should seek to the position of the Seekbar. This code works for mp3 and aac files.
@Override
public void onStopTrackingTouch(SeekBar seekBar){
serviceMain.seekTo(seekBar.getProgress());
}
public void seekTo(int progress) {
MediaPlayerWUri mediaPlayerWUri = getCurrentMediaPlayerWUri(); // This is a MediaPlayer wrapper
if (mediaPlayerWUri != null) { // The correct one is returned // verified by debugger
mediaPlayerWUri.seekTo(progress);
}
}
public void seekTo(int millis){
if (isPrepared) {
mediaPlayer.seekTo(millis); // millis has the correct values // verified via debugger
}
}
I update the Seekbar's max with the media duration; verified it is called with the debugger.
private void updateSeekBar() {
SeekBar seekBar = findViewById(R.id.seekBar);
if (seekBar != null && serviceMain != null) {
final int maxMillis = serviceMain.getCurrentSong().getDuration(getApplicationContext());
seekBar.setMax(maxMillis); // 2305946ms for a file that has the issue
seekBar.setProgress(getCurrentTime());
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
setUpSeekBarUpdater(); // Seek bar updater is a Runnable run every second // code below
}
}
Runnable runnableSeekBarUpdater = new Runnable() {
@Override
public void run() {
if (mediaPlayerWURI.isPlaying()) {
int currentMilliseconds = mediaPlayerWURI.getCurrentPosition();
seekBar.setProgress(currentMilliseconds);
}
}
};
Mkv files do not work with this code; I get weird behavior. If I seek to after the current position of the MediaPlayer with a long media file, the audio stops and it never starts. If I seek to before the current position of the MediaPlayer, the Seekbar goes back for less than a second and jumps back to the current position, and the MediaPlayer does the same thing. Even weirder is that when the audio stops playing, the MediaPlayer keeps running (without updating the Seekbar), and the MediaPlayer will call onCompletion when it reaches the end. Also, the MediaPlayer correctly responds to seekTo(0) after it completes, but when the MediaPlayer gets to 5:50 or somewhere around there, the audio stops.
For shorter mkv files, the seek operation takes about a minute or two where there is no audio playing while it is trying to seek. This is unacceptable. I get the same behavior with long files, where if the seek operation does not complete before the timer reaches the end, the onCompletion is called. I assume this means the long mkv file seeks take so long to complete, that the timer runs to the end before the seek is complete.
The long mkv files give something like the following logs when seeking:
2020-12-01 16:50:35.840 945-12690/? D/NuPlayerDriver: seekTo(0xaf72e8c0) (120991 ms, 3) at state 5
2020-12-01 16:50:35.840 31974-31974/com.example.waveplayer V/ServiceMain: seekTo end
2020-12-01 16:50:35.841 31974-31974/com.example.waveplayer V/ActivityMain: seekTo end
2020-12-01 16:50:35.843 938-938/? E/MatroskaExtractor: Did not locate the video track for seeking
Those logs belong with this encoding
Metadata:
ENCODER : Lavf58.49.100
Duration: 00:38:25.95, start: 0.006000, bitrate: 139 kb/s
Stream #0:0(eng): Audio: vorbis, 192000 Hz, stereo, fltp (default)
Metadata:
ENCODER : Lavc58.98.100 libvorbis
DURATION : 00:38:25.946000000
The numbers are right and are under the max, so I do not know why it is not working.
Is this a bug? mp3 and aac files do not do this.
The short mkv files give a different log output when seeking:
2020-12-01 19:14:19.064 945-26530/? I/NuPlayerDecoder: [OMX.google.vorbis.decoder] resubmitting CSD
2020-12-01 19:14:19.065 945-26530/? I/NuPlayerDecoder: [OMX.google.vorbis.decoder] resubmitting CSD
2020-12-01 19:14:19.066 945-26530/? I/NuPlayerDecoder: [OMX.google.vorbis.decoder] suppressing rendering until 401945000 us
The short mkv has this encoding
Metadata:
MAJOR_BRAND : dash
MINOR_VERSION : 0
COMPATIBLE_BRANDS: iso6avc1mp41
ENCODER : Lavf58.49.100
Duration: 00:18:47.53, start: 0.006000, bitrate: 143 kb/s
Stream #0:0(eng): Audio: vorbis, 192000 Hz, stereo, fltp (default)
Metadata:
ENCODER : Lavc58.98.100 libvorbis
DURATION : 00:18:47.534000000
Is there a way to fix this?