在这个项目中有一个'SimpleExoPlayer',播放器的版本是'exoplayer:r2.5.3'。运行应用程序'SimpleExoPlayer'后,缓冲视频内容并顺利播放。但是用户将'SeekBar'设置为先前位置,'SimpleExoPlayer'重新缓冲以显示视频。对于'.mp4'视频的大文件大小来说,这是一个耗时的过程。非常感谢帮助解决这个问题。
下面是我的代码。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/simple_expo_player"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.exoplayer2.ui.SimpleExoPlayerView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SimpleExoPlayerView simpleExoPlayerView;
SimpleExoPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}
public void initUi(){
// 1. Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create the player
player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector);
// 3. Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), Util.getUserAgent(getApplicationContext(), "com.a3iteam.exoplayertest"));
// 4. Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// 5. This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource(Uri.parse("https://www.w3schools.com/html/mov_bbb.mp4"),dataSourceFactory, extractorsFactory, null, null);
// 6. Prepare the player with the source.
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.simple_expo_player);
simpleExoPlayerView.setPlayer(player);
}
}
答案 0 :(得分:1)
当用户向后搜索时,没有太多阻止玩家缓冲的内容。播放后丢弃过去的数据,您无法轻易更改此行为。
在向后搜索时,可以选择最小化延迟:
支持范围请求是IMO在提供视频文件时必须的,而不仅仅是向后寻求,而是一般寻求。
对于'.mp4'视频的大文件大小来说,这是一个耗时的过程。
在向后搜索时,mp4文件的大小不应影响延迟。您对“大文件大小”的概念让我觉得您的服务器可能不支持http range requests。我可能错了。只想确认一下。 :)
您可以这样检查:
curl -I http://i.imgur.com/z4d4kWk.jpg
HTTP / 1.1 200好的 ... Accept-Ranges:bytes
如果您看到“Accept-Ranges:bytes”标头,则支持范围请求。
ExoPlayer库附带CacheDataSource和相应的CacheDataSourceFactory。因此,您可以通过包装数据源工厂轻松缓存下载的内容:
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
getApplicationContext(), Util.getUserAgent(getApplicationContext(),
"com.a3iteam.exoplayertest"));
Cache cache = new SimpleCache(cacheDir,
new LeastRecentlyUsedCacheEvictor(maxBytes));
dataSourceFactory = new CacheDataSourceFactory(cache,
dataSourceFactory);
当用户向后搜索时,媒体从本地磁盘加载而不是下载,这会减少延迟。不确定您是否通常只为后向搜索用例缓存所有内容。也许将其限制在移动网络上。在使用http范围请求的wifi缓冲应该足够好。
无论哪种方式:尽快删除缓存数据。