android 缓慢加载视频exoplayer

o7jaxewo  于 2023-03-11  发布在  Android
关注(0)|答案(2)|浏览(250)

所以我使用的是ExoPlayer的自定义控制器布局,如果DefaultTimeBar的寻道时间是00:01,而我转到00:03,会有3或4秒的延迟。我试图播放的视频在我的SD卡中。
视频大小为813kb,格式为mp4
我需要增加缓冲区段的大小还是需要一个自定义的负载控制?
玩家:

public class VideoPlayer extends AppCompatActivity {

    PlayerView playerView;
    SimpleExoPlayer simpleExoPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        
    }

    @Override
    protected void onStart() {
        super.onStart();

        playerView = findViewById(R.id.exoPlayer);
        File file = new File("sdcard/" + Environment.DIRECTORY_DOWNLOADS + "/" + "playthis.mp4");
        Uri uri = Uri.fromFile(file);
        Uri uri1 = Uri.parse("file://" + uri);
        LoadControl loadControl = new DefaultLoadControl()
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
        MediaSource mediaSource = buildMediaSource(uri1);
        playerView.setPlayer(simpleExoPlayer);
        playerView.setKeepScreenOn(true);
        simpleExoPlayer.prepare(mediaSource);
        simpleExoPlayer.setPlayWhenReady(true);
        simpleExoPlayer.addListener(new Player.EventListener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if(playWhenReady && playbackState==ExoPlayer.STATE_READY){
                    
                }
                if (playbackState == ExoPlayer.STATE_ENDED) {
                    

                    finish();
                }
            }
        });

       

    }
    public MediaSource buildMediaSource(Uri uri){
        return new ExtractorMediaSource.Factory(new DefaultDataSourceFactory(this,"Test3")).createMediaSource(uri);
    } 
}

自定义控制器的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
             android:layout_height="match_parent">
    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="center" style="@style/ExoMediaButton.Pause"
                 android:src="@drawable/pause_button_increased" android:id="@+id/exo_pause"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="center"
                 android:src="@drawable/play_button_increased"
                 style="@style/ExoMediaButton.Play" android:id="@+id/exo_play" android:cropToPadding="true"
                 android:adjustViewBounds="true"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="start|center" style="@style/ExoMediaButton.Previous"
                 android:layout_marginStart="250dp"
                 android:src="@drawable/prev_button" android:id="@+id/exo_prev"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="end|center" android:layout_marginEnd="250dp"
                 style="@style/ExoMediaButton.Next"
                 android:src="@drawable/next_button" android:id="@+id/exo_next"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="start"
                 android:rotation="180" android:layout_marginStart="15dp"
                 android:layout_marginTop="20dp"
                 android:src="@drawable/settings_icon_back" android:onClick="endActivity" android:background="@null"/>
    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="end" android:layout_marginEnd="10dp"
                 android:layout_marginTop="10dp" android:rotation="90"
                 android:src="@drawable/menu_overflow_vector"
                 android:onClick="addOverlay"
                 android:id="@+id/video_menu" android:background="@null"/>
    <TextView android:layout_width="40dp" android:layout_height="30dp"
              android:layout_gravity="bottom|end"
              android:layout_marginBottom="5dp"
              android:id="@+id/exo_duration" android:textColor="#FFFFFF" android:layout_marginTop="10dp"
              android:layout_marginEnd="72dp"/>
    <TextView android:layout_width="40dp" android:layout_height="30dp"
              android:layout_gravity="start|bottom" android:layout_marginStart="72dp"
              android:layout_marginBottom="5dp" android:layout_marginTop="10dp"
              android:id="@+id/playedTillNow" android:textColor="#FFFFFF"/>

    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
              android:id="@+id/videoName" android:layout_gravity="top" android:layout_marginStart="60dp"
              android:layout_marginTop="20dp" android:textColor="#FFFFFF" android:textStyle="bold"/>

    <com.google.android.exoplayer2.ui.DefaultTimeBar
            android:layout_width="500dp" android:layout_height="26dp"
            android:id="@+id/exo_progress"
            android:layout_gravity="bottom|center"
            app:unplayed_color="#000000"
            android:layout_marginBottom="12dp" app:played_color="@color/crimson"/>

</FrameLayout>

我如何修复延迟?

vxf3dgd4

vxf3dgd41#

请检查:Exoplayer video loading speed
如果你仔细研究答案,你会发现最小化缓冲时间:

//Minimum Video you want to buffer while Playing
private int MIN_BUFFER_DURATION = 2000;
//Max Video you want to buffer during PlayBack
private int MAX_BUFFER_DURATION = 5000;
//Min Video you want to buffer before start Playing it
private int MIN_PLAYBACK_START_BUFFER = 1500;
//Min video You want to buffer when user resumes video
private int MIN_PLAYBACK_RESUME_BUFFER = 2000;

LoadControl loadControl = new DefaultLoadControl.Builder()
    .setAllocator(new DefaultAllocator(true, 16))
    .setBufferDurationsMs(MIN_BUFFER_DURATION,
         MAX_BUFFER_DURATION,
         MIN_PLAYBACK_START_BUFFER,
         MIN_PLAYBACK_RESUME_BUFFER)
    .setTargetBufferBytes(-1)
    .setPrioritizeTimeOverSizeThresholds(true).createDefaultLoadControl();

TrackSelector trackSelector = new DefaultTrackSelector();
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
jei2mxaa

jei2mxaa2#

最好的解决方案是巧妙地设置缓冲区大小。

LoadControl loadControl;

        loadControl = new DefaultLoadControl
                .Builder()
                .setBufferDurationsMs(8*1024, 64*1024, 1024, 1024)
                .setPrioritizeTimeOverSizeThresholds(true)
                .createDefaultLoadControl();

        simpleExoPlayer = new ExoPlayer.Builder(context.getApplicationContext())
                .setLoadControl(loadControl)
                .build();

相关问题