7

While trying to call seekTo function of exoplayer instance the playback is getting reset and playing from start after buffering live audio stream. As some blogs and gists suggest tried calling seekto after pausing exoplayer and then starting the playback again after calling seekto but got no effect on it, its just re-buffering playback and starting again. Below is how i tried so far:

here is my onCreate event where the seekbar events are handled along with exoplayer seekto calls

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playButton = findViewById(R.id.play_button);
    playButton.setOnClickListener(this);
    mSeekBar = findViewById(R.id.seekbar);
    handler.postDelayed( runnable = new Runnable() {
        public void run() {
            //do something
            if (exoPlayer!=null)
            {
                mSeekBar.setMax((int)(exoPlayer.getBufferedPosition()/1000));
                mSeekBar.setProgress((int)(exoPlayer.getCurrentPosition()/1000));
            }
            handler.postDelayed(runnable, delay);
        }
    }, delay);

    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Log.d("SEEKBAR_UPDATE",""+progress);
            if (exoPlayer!=null&&fromUser){
                int duration = exoPlayer.getDuration() == com.google.android.exoplayer2.C.TIME_UNSET ? 0
                        : (int) exoPlayer.getDuration();
                long seekPosition = exoPlayer.getDuration() == com.google.android.exoplayer2.C.TIME_UNSET ? 0
                        : Math.min(Math.max(0, progress*1000), duration);
                exoPlayer.setPlayWhenReady(false);//pause
                exoPlayer.seekTo(seekPosition);
                exoPlayer.setPlayWhenReady(true);
            }
        }
    });
}

here is how my exoplayer is initialized

private void initializePlayer(){
    exoPlayer = new SimpleExoPlayer.Builder(this).build();
    DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer-codelab",5000,2000,true);

    MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(stream_url));
    exoPlayer.prepare(mediaSource);
    Log.d("total duration",""+exoPlayer.getDuration());
    
    exoPlayer.setPlayWhenReady(true);        

}

Please let me know if i am missing something here.

Update 2 :

After reading certain issues related to seeking in exoplayer, it was mentioned in one of the tickets that we need to set this flag LAG_ENABLE_CONSTANT_BITRATE_SEEKING in extractor factory to make the mp3 stream seekable.

DefaultExtractorsFactory extractorsFactory =new DefaultExtractorsFactory()
                        .setMp3ExtractorFlags(Mp3Extractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING)
                        .setConstantBitrateSeekingEnabled(true);

So just to have a basic understanding on how seeking works i tried below where i have an activity with simple exoplayer instance which is passed to player view where i can control the playback and even use the inbuilt seekbar of UI.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    SimpleExoPlayer exoPlayer;
    PlayerView playerView;
    String stream_url_mp3 = "https://sc6.gergosnet.com/puls00HD.mp3";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView = findViewById(R.id.playerView);
        initPlayer();
    }

    private void initPlayer(){
        exoPlayer = new SimpleExoPlayer.Builder(this).build();
        playerView.setPlayer(exoPlayer);
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exo-app"));
        DefaultExtractorsFactory extractorsFactory =new DefaultExtractorsFactory()
                        .setMp3ExtractorFlags(Mp3Extractor.FLAG_ENABLE_CONSTANT_BITRATE_SEEKING)
                        .setConstantBitrateSeekingEnabled(true);
        // This is the MediaSource representing the media to be played.
        MediaSource mediaSource =  new ProgressiveMediaSource.Factory(dataSourceFactory,extractorsFactory)
                        .createMediaSource(Uri.parse(stream_url_mp3));
        // Prepare the player with the source.
        exoPlayer.prepare(mediaSource);

    }
}

MainLayout.xml contains Player Ui component

<com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

even after adding the FLAG_ENABLE_CONSTANT_BITRATE_SEEKING and setting mp3 extractor i am not able to use the seekbar on the player view UI.

Please help!

MDT
  • 1,535
  • 3
  • 16
  • 29
  • the URL you provided is a live stream, what do you want to achieve with seeking? live streams do not have a duration so usually, you shouldn't seek, because the data is loaded as playlists. – oziomajnr Jul 22 '20 at 04:40
  • see https://github.com/google/ExoPlayer/issues/4917 – oziomajnr Jul 22 '20 at 04:41
  • @oziomajnr this stream is perfectly seekable in mediaplayer api which is the base that exoplayer is built on, i have seen exoplayer used in some prominent radio players and they are able to seek perfectly fine. – MDT Jul 22 '20 at 11:11
  • @oziomajnr they made live stream seekable as per my understanding [here](https://github.com/google/ExoPlayer/issues/6787) and i am using the same mp3 extractor to make it seekable but its not working and i cant say why – MDT Jul 22 '20 at 11:14
  • I believe the stream must be seekable in order to support seeking. I used to work with exoplayer to play live streams and it worked out of the box. – Naveen Dissanayake Jul 27 '20 at 13:49
  • 1
    the stream is completely seekable and i have a alternative thing going on in case this approach tanks. I am able to seek it if i am downloading the stream and play it as a file. So i guess there is nothing wrong with the mp3 stream. – MDT Aug 02 '20 at 06:06
  • @MDT Exoplayer is not built on `MediaPlayer`. From docs: `ExoPlayer’s standard audio and video components are built on Android’s MediaCodec API` – Starwave Sep 21 '22 at 14:39

0 Answers0