com.google.android.exoplayer2.upstream.RawResourceDataSource Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.RawResourceDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From ExoplayerExample with The Unlicense 6 votes vote down vote up
private void prepareExoPlayerFromRawResourceUri(Uri uri){
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);

    DataSpec dataSpec = new DataSpec(uri);
    final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
    try {
        rawResourceDataSource.open(dataSpec);
    } catch (RawResourceDataSource.RawResourceDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return rawResourceDataSource;
        }
    };

    MediaSource audioSource = new ExtractorMediaSource(rawResourceDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);

    exoPlayer.prepare(audioSource);
    initMediaControls();
}
 
Example #2
Source File: ExoSourceManager.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
/**
 * @param dataSource  链接
 * @param preview     是否带上header,默认有header自动设置为true
 * @param cacheEnable 是否需要缓存
 * @param isLooping   是否循环
 * @param cacheDir    自定义缓存目录
 */
public MediaSource getMediaSource(String dataSource, boolean preview, boolean cacheEnable, boolean isLooping, File cacheDir, @Nullable String overrideExtension) {
    MediaSource mediaSource = null;
    if (sExoMediaSourceInterceptListener != null) {
        mediaSource = sExoMediaSourceInterceptListener.getMediaSource(dataSource, preview, cacheEnable, isLooping, cacheDir);
    }
    if (mediaSource != null) {
        return mediaSource;
    }
    mDataSource = dataSource;
    Uri contentUri = Uri.parse(dataSource);
    int contentType = inferContentType(dataSource, overrideExtension);

    String uerAgent = null;
    if (mMapHeadData != null) {
        uerAgent = mMapHeadData.get("User-Agent");
    }
    if ("android.resource".equals(contentUri.getScheme())) {
        DataSpec dataSpec = new DataSpec(contentUri);
        final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(mAppContext);
        try {
            rawResourceDataSource.open(dataSpec);
        } catch (RawResourceDataSource.RawResourceDataSourceException e) {
            e.printStackTrace();
        }
        DataSource.Factory factory = new DataSource.Factory() {
            @Override
            public DataSource createDataSource() {
                return rawResourceDataSource;
            }
        };
        return new ProgressiveMediaSource.Factory(
                factory).createMediaSource(contentUri);

    }

    switch (contentType) {
        case C.TYPE_SS:
            mediaSource = new SsMediaSource.Factory(
                    new DefaultSsChunkSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir, uerAgent)),
                    new DefaultDataSourceFactory(mAppContext, null,
                            getHttpDataSourceFactory(mAppContext, preview, uerAgent))).createMediaSource(contentUri);
            break;
        case C.TYPE_DASH:
            mediaSource = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir, uerAgent)),
                    new DefaultDataSourceFactory(mAppContext, null,
                            getHttpDataSourceFactory(mAppContext, preview, uerAgent))).createMediaSource(contentUri);
            break;
        case C.TYPE_HLS:
            mediaSource = new HlsMediaSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable, preview, cacheDir, uerAgent)).createMediaSource(contentUri);
            break;
        case TYPE_RTMP:
            RtmpDataSourceFactory rtmpDataSourceFactory = new RtmpDataSourceFactory(null);
            mediaSource = new ProgressiveMediaSource.Factory(rtmpDataSourceFactory,
                    new DefaultExtractorsFactory())
                    .createMediaSource(contentUri);
            break;
        case C.TYPE_OTHER:
        default:
            mediaSource = new ProgressiveMediaSource.Factory(getDataSourceFactoryCache(mAppContext, cacheEnable,
                    preview, cacheDir, uerAgent), new DefaultExtractorsFactory())
                    .createMediaSource(contentUri);
            break;
    }
    if (isLooping) {
        return new LoopingMediaSource(mediaSource);
    }
    return mediaSource;
}
 
Example #3
Source File: RawResourceDataSourceFactory.java    From react-native-video with MIT License 4 votes vote down vote up
@Override
public DataSource createDataSource() {
    return new RawResourceDataSource(context);
}