其他分享
首页 > 其他分享> > Android ExoPlayer – 同时下载视频(非DASH / HLS)和流媒体

Android ExoPlayer – 同时下载视频(非DASH / HLS)和流媒体

作者:互联网

我想下载一个在ExoPlayer中流式传输的视频.

另外,甚至在使用ExoPlayer之前,我从HttpURLConnection提供的输入流中下载了一个文件,并从本地存储播放该文件.这没关系,但是它并没有解决我的同步流和缓存问题.

ExoPlayer还提供了一个缓存系统,这些系统似乎只适用于DASH或HLS流类型.我没有使用这些,并希望使用ExtractorRendererBuilder缓存mp4. (这个主题在这里有相当广泛的内容:https://github.com/google/ExoPlayer/issues/420).

DefaultHttpDataSource确实有一个公开HttpURLConnection的api,但我不确定我是否正在重用该流.以下是ExoPlayer中提供的示例代码.

    @Override
    public void buildRenderers(DemoPlayer player) {
        Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
        Handler mainHandler = player.getMainHandler();

        // Build the video and audio renderers.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(mainHandler, null);
        DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter,userAgent);
        ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri,dataSource,allocator,
                BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE, mainHandler, player, 0);
        MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(context,
                sampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000,
                mainHandler, player, 50);
        MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,
                MediaCodecSelector.DEFAULT, null, true, mainHandler, player,
                AudioCapabilities.getCapabilities(context), AudioManager.STREAM_MUSIC);
        TrackRenderer textRenderer = new TextTrackRenderer(sampleSource, player,
                mainHandler.getLooper());

        // Invoke the callback.
        TrackRenderer[] renderers = new TrackRenderer[DemoPlayer.RENDERER_COUNT];
        renderers[DemoPlayer.TYPE_VIDEO] = videoRenderer;
        renderers[DemoPlayer.TYPE_AUDIO] = audioRenderer;
        renderers[DemoPlayer.TYPE_TEXT] = textRenderer;
        player.onRenderers(renderers, bandwidthMeter);
    }

我现在做的是扩展DefaultHttpDataSource并使用HttpURLConnection获取一个InputStream,写入getCacheDir()中的文件.这是扩展DefaultHttpDataSource的类:

public class CachedHttpDataSource extends DefaultHttpDataSource {
    public CachedHttpDataSource(String userAgent, Predicate<String> contentTypePredicate) {
        super(userAgent, contentTypePredicate);
    }

    public CachedHttpDataSource(String userAgent, Predicate<String> contentTypePredicate, TransferListener listener) {
        super(userAgent, contentTypePredicate, listener);
    }

    public CachedHttpDataSource(String userAgent, Predicate<String> contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis) {
        super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis);
    }

    public CachedHttpDataSource(String userAgent, Predicate<String> contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects) {
        super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis, allowCrossProtocolRedirects);
    }

    public HttpURLConnection getURLConnection(){
        HttpURLConnection connection = getConnection();

        return getConnection();
    }
}

我现在可以通过getURLConnection()获取一个InputStream来将视频保存到文件中,但我真的很高兴再次使用InputStream来缓存视频.是否有任何其他API /或方法可以访问字节数组,我可以在流式传输时写入文件?

我的其他stackoverflow搜索尚未提供解决方案:

Using cache in ExoPlayer

ExoPlayer cache

感谢您的时间和耐心.

解决方法:

我找到了一个只有一个问题的解决方案:向后或向前寻找是行不通的.

这是一段代码:

public void preparePlayer(String videoUri) {
    MediaSource videoSource =
            new ExtractorMediaSource( Uri.parse( videoUri ), dataSourceFactory, extractorsFactory, handler, null );
    exoPlayer.prepare( videoSource );
    exoPlayer.setPlayWhenReady( true );
}

public DataSource.Factory buildDataSourceFactory() {
    return new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor( CACHE_SIZE_BYTES );
            File cacheDir = //Your cache dir
            SimpleCache simpleCache = new SimpleCache( cacheDir, evictor );
            DataSource dataSource = buildMyDataSourceFactory().createDataSource();
            int cacheFlags = CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_CACHE_UNBOUNDED_REQUESTS;
            return new CacheDataSource( simpleCache, dataSource, cacheFlags, CACHE_SIZE_BYTES );
        }
    };
}

private DefaultDataSource.Factory buildMyDataSourceFactory() {
    return new DefaultDataSourceFactory( context, "jesty-android", new DefaultBandwidthMeter() );
}

资料来源:https://github.com/google/ExoPlayer/issues/420#issuecomment-244652023

标签:offline-caching,android,exoplayer
来源: https://codeday.me/bug/20191005/1857011.html