Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从第二个活动返回导航后,如何在没有缓冲的情况下播放视频?_Android_Android Videoview - Fatal编程技术网

Android 从第二个活动返回导航后,如何在没有缓冲的情况下播放视频?

Android 从第二个活动返回导航后,如何在没有缓冲的情况下播放视频?,android,android-videoview,Android,Android Videoview,我正在为背景视频播放编写代码,为此我使用了VideoView。我正在从服务器获取视频URL,然后将其解析为videoView。原因很明显,加载需要时间 private String videoViewUrl = null; private VideoView videoView; private MediaPlayer mp; networkManager.getRequest(JSON_URL, new NetworkCallback() { @Override

我正在为背景视频播放编写代码,为此我使用了
VideoView
。我正在从服务器获取视频URL,然后将其解析为
videoView
。原因很明显,加载需要时间

private String videoViewUrl = null;
private VideoView videoView;
private MediaPlayer mp;

networkManager.getRequest(JSON_URL, new NetworkCallback() {
        @Override
        public void onSuccess(String result) {
            try {

                JSONObject obj = new JSONObject(result);
                JSONObject themeObj = obj.getJSONObject("Theme");
                JSONObject videoObj = themeObj.getJSONObject("Video");
                videoViewUrl = videoObj.getString("VideoUrl");
                // parse to videoview
                videoView.setVideoURI(Uri.parse(videoViewUrl));
                videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mp = mediaPlayer;
                        mediaPlayer.setLooping(true);
                    }
                });

                videoView.start();


@Override
protected void onResume() {
    if (videoView != null)
        videoView.resume();
    super.onResume();


@Override
protected void onPause() {
    if (videoView != null)
        videoView.suspend();
    super.onPause();

所以,每当我开始新的活动并返回时,视频就会重新启动并需要时间重新加载。有没有办法把缓冲时间减到最少

对于使用此类的用户,可以使用缓存:

public class CacheDataSourceFactory implements DataSource.Factory {
private final Context context;
private final DefaultDataSourceFactory defaultDatasourceFactory;
private final long maxFileSize, maxCacheSize;
private static SimpleCache simpleCache;


public static SimpleCache getInstance(Context c, long maxCacheSize) {
    if (simpleCache == null) simpleCache = new SimpleCache(new File(c.getCacheDir(), "exoCache"), new LeastRecentlyUsedCacheEvictor(maxCacheSize));
    return simpleCache;
}

public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) {
    super();
    this.context = context;
    this.maxCacheSize = maxCacheSize;
    this.maxFileSize = maxFileSize;
    String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    defaultDatasourceFactory = new DefaultDataSourceFactory(this.context,
            bandwidthMeter,
            new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter));
}

@Override
public DataSource createDataSource() {
    return new CacheDataSource(CacheDataSourceFactory.getInstance(context, maxCacheSize), defaultDatasourceFactory.createDataSource(),
            new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
            CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
}}
SimpleCache应该是单例的,否则它将覆盖缓存 每次都是

现在,在你这样的活动中使用它

 BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

        exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        MediaSource mSource = new ExtractorMediaSource(Uri.parse(videoViewUrl),
                new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);

        exoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
        exoPlayerView.setPlayer(exoPlayer);
        exoPlayer.setRepeatMode(Player.REPEAT_MODE_ALL); // if you want
        exoPlayer.prepare(mSource);
        exoPlayer.setPlayWhenReady(true);
这会很有效的