seekTo restarting playback on exoplayer

社会主义新天地 提交于 2020-12-03 07:32:28

问题


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!

来源:https://stackoverflow.com/questions/62979193/seekto-restarting-playback-on-exoplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!