com.google.android.exoplayer2.source.ExtractorMediaSource Java Examples

The following examples show how to use com.google.android.exoplayer2.source.ExtractorMediaSource. 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: ExoPlayerFragment.java    From carstream-android-auto with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(PlayerQueue playerQueue) {
    ArrayList<MediaSource> mediaSources = new ArrayList<>();
    File[] currentQueue = playerQueue.getCurrentQueue();
    for (File file : currentQueue) {
        Uri fileUri = Uri.fromFile(file);
        String userAgent = Util.getUserAgent(getContext(), "CarStream");
        if (file != null && (file.getName().endsWith(".m3u") || file.getName().endsWith(".m3u8"))) {
            Handler mHandler = new Handler();
            DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), userAgent);
            HlsMediaSource mediaSource = new HlsMediaSource(fileUri, defaultDataSourceFactory, 1800000,
                    mHandler, null);
            mediaSources.add(mediaSource);
        } else {
            ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource(fileUri,
                    new DefaultDataSourceFactory(getContext(), userAgent),
                    new DefaultExtractorsFactory(), null, null);
            mediaSources.add(extractorMediaSource);
        }
    }
    ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource(mediaSources.toArray(new MediaSource[mediaSources.size()]));
    return concatenatingMediaSource;
}
 
Example #2
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 #3
Source File: MainActivity.java    From ExoplayerExample with The Unlicense 6 votes vote down vote up
/**
 * Prepares exoplayer for audio playback from a remote URL audiofile. Should work with most
 * popular audiofile types (.mp3, .m4a,...)
 * @param uri Provide a Uri in a form of Uri.parse("http://blabla.bleble.com/blublu.mp3)
 */
private void prepareExoPlayerFromURL(Uri uri){

    TrackSelector trackSelector = new DefaultTrackSelector();

    LoadControl loadControl = new DefaultLoadControl();

    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), null);
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    MediaSource audioSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null);
    exoPlayer.addListener(eventListener);

    exoPlayer.prepare(audioSource);
    initMediaControls();
}
 
Example #4
Source File: MainActivity.java    From ExoplayerExample with The Unlicense 6 votes vote down vote up
/**
 * Prepares exoplayer for audio playback from a local file
 * @param uri
 */
private void prepareExoPlayerFromFileUri(Uri uri){
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);

    DataSpec dataSpec = new DataSpec(uri);
    final FileDataSource fileDataSource = new FileDataSource();
    try {
        fileDataSource.open(dataSpec);
    } catch (FileDataSource.FileDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return fileDataSource;
        }
    };
    MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);

    exoPlayer.prepare(audioSource);
    initMediaControls();
}
 
Example #5
Source File: MediaPlayback21.java    From Melophile with Apache License 2.0 6 votes vote down vote up
@Override
public void startPlayer() {
  if (exoPlayer == null) {
    exoPlayer =
            ExoPlayerFactory.newSimpleInstance(
                    context, new DefaultTrackSelector(), new DefaultLoadControl());
    exoPlayer.addListener(this);
  }
  exoPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
          context, Util.getUserAgent(context, "uamp"), null);
  ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
  MediaSource mediaSource = new ExtractorMediaSource(
          Uri.parse(currentUrl), dataSourceFactory, extractorsFactory, null, null);
  exoPlayer.prepare(mediaSource);
  configPlayer();
}
 
Example #6
Source File: ExoMedia.java    From QSVideoPlayer with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Context context, Uri uri) {
    int type = getUrlType(uri.toString());
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource(uri, new DefaultDataSourceFactory(context, null,
                    new DefaultHttpDataSourceFactory(USER_AGENT, null)),
                    new DefaultSsChunkSource.Factory(new DefaultDataSourceFactory(context, BANDWIDTH_METER,
                            new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER))), mainThreadHandler, null);
        case C.TYPE_DASH:
            return new DashMediaSource(uri, new DefaultDataSourceFactory(context, null,
                    new DefaultHttpDataSourceFactory(USER_AGENT, null)),
                    new DefaultDashChunkSource.Factory(new DefaultDataSourceFactory(context, BANDWIDTH_METER,
                            new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER))), mainThreadHandler, null);
        case C.TYPE_HLS:
            return new HlsMediaSource(uri, new DefaultDataSourceFactory(context, BANDWIDTH_METER,
                    new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER)), mainThreadHandler, null);
        case C.TYPE_OTHER:
            return new ExtractorMediaSource(uri, new DefaultDataSourceFactory(context, BANDWIDTH_METER,
                    new DefaultHttpDataSourceFactory(USER_AGENT, BANDWIDTH_METER)), new DefaultExtractorsFactory(),
                    mainThreadHandler, null);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #7
Source File: PlayerActivity.java    From ExoPlayer-Offline with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
    int type = Util.inferContentType(!TextUtils.isEmpty(overrideExtension) ? "." + overrideExtension
            : uri.getLastPathSegment());
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource(uri, buildDataSourceFactory(false),
                    new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
        case C.TYPE_DASH:
            return new DashMediaSource(uri, buildDataSourceFactory(false),
                    new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
        case C.TYPE_HLS:
            return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
        case C.TYPE_OTHER:
            return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
                    mainHandler, eventLogger);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #8
Source File: LiveVideoPlayerActivity.java    From LiveVideoBroadcaster with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
  int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
          : Util.inferContentType("." + overrideExtension);
  switch (type) {
    case C.TYPE_SS:
      return new SsMediaSource(uri, buildDataSourceFactory(false),
              new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
    case C.TYPE_DASH:
      return new DashMediaSource(uri, buildDataSourceFactory(false),
              new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
    case C.TYPE_HLS:
      return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
    case C.TYPE_OTHER:
      if (uri.getScheme().equals("rtmp")) {
        return new ExtractorMediaSource(uri, rtmpDataSourceFactory, new DefaultExtractorsFactoryForFLV(),
                mainHandler, eventLogger);
      }
      else {
        return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
                mainHandler, eventLogger);
      }
    default: {
      throw new IllegalStateException("Unsupported type: " + type);
    }
  }
}
 
Example #9
Source File: VideoPlayerComponent.java    From android-arch-components-lifecycle with Apache License 2.0 6 votes vote down vote up
private void initializePlayer() {
    if (player == null) {
        trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
        player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
        player.addListener(this);
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, "testApp"), bandwidthMeter);

        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        ExtractorMediaSource videoSource = new ExtractorMediaSource(Uri.parse(videoUrl),
                dataSourceFactory, extractorsFactory, null, null);
        simpleExoPlayerView.setPlayer(player);
        player.setPlayWhenReady(true);

        boolean haveResumePosition = resumeWindow != C.INDEX_UNSET;
        if (haveResumePosition) {
            Log.d(TAG, "Have Resume position true!" + resumePosition);
            player.seekTo(resumeWindow, resumePosition);
        }

        player.prepare(videoSource, !haveResumePosition, false);

    }
}
 
Example #10
Source File: AdsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a new source that inserts ads linearly with the content specified by {@code
 * contentMediaSource}. Ad media is loaded using {@link ExtractorMediaSource}.
 *
 * @param contentMediaSource The {@link MediaSource} providing the content to play.
 * @param dataSourceFactory Factory for data sources used to load ad media.
 * @param adsLoader The loader for ads.
 * @param adUiViewGroup A {@link ViewGroup} on top of the player that will show any ad UI.
 * @param eventHandler A handler for events. May be null if delivery of events is not required.
 * @param eventListener A listener of events. May be null if delivery of events is not required.
 * @deprecated To listen for ad load error events, add a listener via {@link
 *     #addEventListener(Handler, MediaSourceEventListener)} and check for {@link
 *     AdLoadException}s in {@link MediaSourceEventListener#onLoadError(int, MediaPeriodId,
 *     LoadEventInfo, MediaLoadData, IOException, boolean)}. Individual ads loader implementations
 *     should expose ad interaction events, if applicable.
 */
@Deprecated
public AdsMediaSource(
    MediaSource contentMediaSource,
    DataSource.Factory dataSourceFactory,
    AdsLoader adsLoader,
    ViewGroup adUiViewGroup,
    @Nullable Handler eventHandler,
    @Nullable EventListener eventListener) {
  this(
      contentMediaSource,
      new ExtractorMediaSource.Factory(dataSourceFactory),
      adsLoader,
      adUiViewGroup,
      eventHandler,
      eventListener);
}
 
Example #11
Source File: ExoMediaPlayer.java    From VideoDemoJava with MIT License 6 votes vote down vote up
private MediaSource getMediaSource(Uri uri){
    int contentType = Util.inferContentType(uri);
    DefaultDataSourceFactory dataSourceFactory =
            new DefaultDataSourceFactory(mAppContext,
                    Util.getUserAgent(mAppContext, mAppContext.getPackageName()), mBandwidthMeter);
    switch (contentType) {
        case C.TYPE_DASH:
            DefaultDashChunkSource.Factory factory = new DefaultDashChunkSource.Factory(dataSourceFactory);
            return new DashMediaSource(uri, dataSourceFactory, factory, null, null);
        case C.TYPE_SS:
            DefaultSsChunkSource.Factory ssFactory = new DefaultSsChunkSource.Factory(dataSourceFactory);
            return new SsMediaSource(uri, dataSourceFactory, ssFactory, null, null);
        case C.TYPE_HLS:
            return new HlsMediaSource(uri, dataSourceFactory, null, null);

        case C.TYPE_OTHER:
        default:
            // This is the MediaSource representing the media to be played.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
            return new ExtractorMediaSource(uri,
                    dataSourceFactory, extractorsFactory, null, null);
    }
}
 
Example #12
Source File: RNRtmpView.java    From react-native-rtmpview with MIT License 6 votes vote down vote up
public void play() {
    if (mPlayer != null && mPlayer.getPlaybackState() == Player.STATE_IDLE) {

        if (this.mTransferListener != null) {
            this.mTransferListener.dispose();
            this.mTransferListener = null;
        }

        mTransferListener = new RNRtmpTransferListener(this);

        String rtmpUrl = this.mUrlString;
        RtmpDataSourceFactory rtmpDataSourceFactory = new RtmpDataSourceFactory(mTransferListener);
        MediaSource videoSource = new ExtractorMediaSource.Factory(rtmpDataSourceFactory)
                .createMediaSource(Uri.parse(rtmpUrl + " live=1"));
        mPlayer.prepare(videoSource);

        mPlayer.setPlayWhenReady(true);
    } else {
        Log.i(APP_NAME, "Unable to play; not idle");
    }
}
 
Example #13
Source File: VideoExoPlayer.java    From TigerVideo with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {

        int type = Util.inferContentType(!TextUtils.isEmpty(overrideExtension) ? "." + overrideExtension
                : uri.getLastPathSegment());
        switch (type) {
            case C.TYPE_SS:
                return new SsMediaSource(uri, buildDataSourceFactory(false),
                        new DefaultSsChunkSource.Factory(mMediaDataSourceFactory), mMainHandler, mEventLogger);
            case C.TYPE_DASH:
                return new DashMediaSource(uri, buildDataSourceFactory(false),
                        new DefaultDashChunkSource.Factory(mMediaDataSourceFactory), mMainHandler, mEventLogger);
            case C.TYPE_HLS:
                return new HlsMediaSource(uri, mMediaDataSourceFactory, mMainHandler, mEventLogger);
            case C.TYPE_OTHER:
                return new ExtractorMediaSource(uri, mMediaDataSourceFactory, new DefaultExtractorsFactory(),
                        mMainHandler, mEventLogger);
            default: {
                throw new IllegalStateException("Unsupported type: " + type);
            }
        }
    }
 
Example #14
Source File: MediaPlayerFragment.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
	int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
		: Util.inferContentType("." + overrideExtension);
	switch (type) {
		case C.TYPE_SS:
			return new SsMediaSource(uri, buildDataSourceFactory(false),
									 new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
		case C.TYPE_DASH:
			return new DashMediaSource(uri, buildDataSourceFactory(false),
									   new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
		case C.TYPE_HLS:
			return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
		case C.TYPE_OTHER:
			return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
											mainHandler, eventLogger);
		default: {
				throw new IllegalStateException("Unsupported type: " + type);
			}
	}
}
 
Example #15
Source File: Player.java    From zapp with MIT License 6 votes vote down vote up
@NonNull
private MediaSource getMediaSourceWithoutSubtitles(Uri uri) {
	int type = Util.inferContentType(uri);
	switch (type) {
		case C.TYPE_HLS:
			return new HlsMediaSource.Factory(dataSourceFactory)
				.createMediaSource(uri);
		case C.TYPE_OTHER:
			return new ExtractorMediaSource.Factory(dataSourceFactory)
				.createMediaSource(uri);
		case C.TYPE_DASH:
		case C.TYPE_SS:
		default:
			throw new IllegalStateException("Unsupported type: " + type);
	}
}
 
Example #16
Source File: KExoMediaPlayer.java    From K-Sonic with MIT License 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
    int type = Util.inferContentType(!TextUtils.isEmpty(overrideExtension) ? "." + overrideExtension
            : uri.getLastPathSegment());
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource(uri, new DefaultDataSourceFactory(context, userAgent),
                    new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
        case C.TYPE_DASH:
            return new DashMediaSource(uri, new DefaultDataSourceFactory(context, userAgent),
                    new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
        case C.TYPE_HLS:
            return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
        case C.TYPE_OTHER:
            return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
                    mainHandler, eventLogger);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #17
Source File: AdsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a new source that inserts ads linearly with the content specified by {@code
 * contentMediaSource}. Ad media is loaded using {@link ExtractorMediaSource}.
 *
 * @param contentMediaSource The {@link MediaSource} providing the content to play.
 * @param dataSourceFactory Factory for data sources used to load ad media.
 * @param adsLoader The loader for ads.
 * @param adUiViewGroup A {@link ViewGroup} on top of the player that will show any ad UI.
 * @param eventHandler A handler for events. May be null if delivery of events is not required.
 * @param eventListener A listener of events. May be null if delivery of events is not required.
 * @deprecated To listen for ad load error events, add a listener via {@link
 *     #addEventListener(Handler, MediaSourceEventListener)} and check for {@link
 *     AdLoadException}s in {@link MediaSourceEventListener#onLoadError(int, MediaPeriodId,
 *     LoadEventInfo, MediaLoadData, IOException, boolean)}. Individual ads loader implementations
 *     should expose ad interaction events, if applicable.
 */
@Deprecated
public AdsMediaSource(
    MediaSource contentMediaSource,
    DataSource.Factory dataSourceFactory,
    AdsLoader adsLoader,
    ViewGroup adUiViewGroup,
    @Nullable Handler eventHandler,
    @Nullable EventListener eventListener) {
  this(
      contentMediaSource,
      new ExtractorMediaSource.Factory(dataSourceFactory),
      adsLoader,
      adUiViewGroup,
      eventHandler,
      eventListener);
}
 
Example #18
Source File: PlayerActivity.java    From GPUVideo-android with MIT License 6 votes vote down vote up
private void setUpSimpleExoPlayer() {

        TrackSelector trackSelector = new DefaultTrackSelector();

        // Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"), defaultBandwidthMeter);
        MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(STREAM_URL_MP4_VOD_LONG));

        // SimpleExoPlayer
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        // Prepare the player with the source.
        player.prepare(mediaSource);
        player.setPlayWhenReady(true);

    }
 
Example #19
Source File: VideoPlayer.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
private void setExoViewSource(@NonNull VideoSlide videoSource, boolean autoplay)
    throws IOException
{
  BandwidthMeter         bandwidthMeter             = new DefaultBandwidthMeter();
  TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
  TrackSelector          trackSelector              = new DefaultTrackSelector(videoTrackSelectionFactory);
  LoadControl            loadControl                = new DefaultLoadControl();

  exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
  exoPlayer.addListener(new ExoPlayerListener(window));
  //noinspection ConstantConditions
  exoView.setPlayer(exoPlayer);

  DefaultDataSourceFactory    defaultDataSourceFactory    = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null);
  AttachmentDataSourceFactory attachmentDataSourceFactory = new AttachmentDataSourceFactory(getContext(), defaultDataSourceFactory, null);
  ExtractorsFactory           extractorsFactory           = new DefaultExtractorsFactory();

  MediaSource mediaSource = new ExtractorMediaSource(videoSource.getUri(), attachmentDataSourceFactory, extractorsFactory, null, null);

  exoPlayer.prepare(mediaSource);
  exoPlayer.setPlayWhenReady(autoplay);
}
 
Example #20
Source File: VideoPlayer.java    From media_player with MIT License 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, DataSource.Factory mediaDataSourceFactory, Context context) {
    int type = Util.inferContentType(uri.getLastPathSegment());
    switch (type) {
    case C.TYPE_SS:
        return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
                new DefaultDataSourceFactory(context, null, mediaDataSourceFactory)).createMediaSource(uri);
    case C.TYPE_DASH:
        return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
                new DefaultDataSourceFactory(context, null, mediaDataSourceFactory)).createMediaSource(uri);
    case C.TYPE_HLS:
        return new HlsMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);
    case C.TYPE_OTHER:
        return new ExtractorMediaSource.Factory(mediaDataSourceFactory)
                .setExtractorsFactory(new DefaultExtractorsFactory()).createMediaSource(uri);
    default: {
        throw new IllegalStateException("Unsupported type: " + type);
    }
    }
}
 
Example #21
Source File: PlayerActivity.java    From leafpicrevived with GNU General Public License v3.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
        int type = Util.inferContentType(!TextUtils.isEmpty(overrideExtension) ? "." + overrideExtension : uri.getLastPathSegment());
        switch (type) {
            case C.TYPE_SS:
                return new SsMediaSource(uri, buildDataSourceFactory(false), new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, null);
            case C.TYPE_OTHER:
                return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(), mainHandler, null);
            case C.TYPE_DASH:
                return new DashMediaSource(uri, buildDataSourceFactory(false), new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, null);
//            case C.TYPE_HLS:return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, null);
            case C.TYPE_HLS:
                throw new IllegalStateException("Unsupported type: " + type);
            default:
                throw new IllegalStateException("Unsupported type: " + type);
        }
    }
 
Example #22
Source File: YouTubePlayerV2Fragment.java    From SkyTube with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Play video.
 *
 * @param videoUri  The Uri of the video that is going to be played.
 */
private void playVideo(Uri videoUri) {
	DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getContext(), "ST. Agent", new DefaultBandwidthMeter());
	ExtractorMediaSource.Factory extMediaSourceFactory = new ExtractorMediaSource.Factory(dataSourceFactory);
	ExtractorMediaSource mediaSource = extMediaSourceFactory.createMediaSource(videoUri);
	player.prepare(mediaSource);

	if (playerInitialPosition > 0)
		player.seekTo(playerInitialPosition);
}
 
Example #23
Source File: PlaybackFragment.java    From androidtv-Leanback with Apache License 2.0 5 votes vote down vote up
private void prepareMediaForPlaying(Uri mediaSourceUri) {
    String userAgent = Util.getUserAgent(getActivity(), "VideoPlayerGlue");
    MediaSource mediaSource =
            new ExtractorMediaSource(
                    mediaSourceUri,
                    new DefaultDataSourceFactory(getActivity(), userAgent),
                    new DefaultExtractorsFactory(),
                    null,
                    null);

    mPlayer.prepare(mediaSource);
}
 
Example #24
Source File: VideoPlayer.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Function that provides the media source played by ExoPlayer based on media type.
 *
 * @param videoUrl Video URL
 * @return The {@link MediaSource} to play.
 */
private MediaSource getMediaSource(String videoUrl) {
    final String userAgent = Util.getUserAgent(this.context, this.context.getString(R.string.app_name));
    final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this.context, userAgent);
    final MediaSource mediaSource;

    if (VideoUtil.videoHasFormat(videoUrl, VIDEO_FORMAT_M3U8)) {
        mediaSource = new HlsMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(videoUrl));
    } else {
        mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(videoUrl));
    }
    return mediaSource;
}
 
Example #25
Source File: MediaSourceFactory.java    From CumulusTV with MIT License 5 votes vote down vote up
public static MediaSource getMediaSourceFor(Context context, Uri mediaUri,
        String overrideExtension) {
    // Measures bandwidth during playback. Can be null if not required.
    DataSource.Factory mediaDataSourceFactory = buildDataSourceFactory(context, true);

    int type = Util.inferContentType(!TextUtils.isEmpty(overrideExtension) ?
            "." + overrideExtension
            : mediaUri.getLastPathSegment());
    Handler mainHandler = new Handler();
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource(mediaUri, buildDataSourceFactory(context, false),
                    new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
                    mainHandler, null);
        case C.TYPE_DASH:
            return new DashMediaSource(mediaUri, buildDataSourceFactory(context, false),
                    new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler,
                    null);
        case C.TYPE_HLS:
            return new HlsMediaSource(mediaUri, mediaDataSourceFactory, mainHandler,
                    null);
        case C.TYPE_OTHER:
            return new ExtractorMediaSource(mediaUri, mediaDataSourceFactory,
                    new DefaultExtractorsFactory(), mainHandler, null);
        default: {
            throw new NotMediaException("Unsupported type: " + type);
        }
    }
}
 
Example #26
Source File: MediaPlayerSingleton.java    From dtube-mobile-unofficial with Apache License 2.0 5 votes vote down vote up
private MediaSource getMediaSource(String url){
    Uri uri = Uri.parse(url);

    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(c,
            Util.getUserAgent(c, "DtubeClient"));
    // This is the MediaSource representing the media to be played.
    return new ExtractorMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
}
 
Example #27
Source File: VodPlaybackFragment.java    From xipl with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the usage of the internal player used by the library.
 */
protected void configureInternalPlayer() {
    Bundle arguments = getArguments();
    String url = arguments.getString(VodTvSectionFragment.AV_CONTENT_LINK_BUNDLE);

    // Configure the ExoPlayer instance that will be used to play the media
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getActivity(), Util.getUserAgent(getActivity(), getActivity().getApplicationInfo().loadLabel(getActivity().getPackageManager()).toString()));
    mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getActivity()), new DefaultTrackSelector(), new DefaultLoadControl());
    ExtractorMediaSource.Factory factory = new ExtractorMediaSource.Factory(dataSourceFactory);
    Uri uri = Uri.parse(url);
    mSimpleExoPlayer.prepare(factory.createMediaSource(uri));
    mSimpleExoPlayer.addListener(new Player.DefaultEventListener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            super.onPlayerStateChanged(playWhenReady, playbackState);
            if (playbackState == Player.STATE_READY && mPlayerGlue.getSeekProvider() == null) {
                mPlayerGlue.setSeekProvider(new ProviderPlaybackSeekDataProvider(mPlayerGlue.getDuration()));

                // Force content to fit to screen if wanted.
                if (getVodProperties().isVideoFitToScreen()) {
                    DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
                    mPlayerGlue.getPlayerAdapter().getCallback().onVideoSizeChanged(mPlayerGlue.getPlayerAdapter(), displayMetrics.widthPixels, displayMetrics.heightPixels);
                }
            }
        }
    });

    // Configure Leanback for playback. Use the updatePeriodMs used before in ExoPlayerAdapter
    LeanbackPlayerAdapter playerAdapter = new LeanbackPlayerAdapter(getActivity(), mSimpleExoPlayer, 16);
    mPlayerGlue = new PlaybackTransportControlGlue<>(getActivity(), playerAdapter);
    mPlayerGlue.setHost(new VideoSupportFragmentGlueHost(this));
    mPlayerGlue.setTitle(arguments.getString(VodTvSectionFragment.AV_CONTENT_TITLE_BUNDLE));
    mPlayerGlue.setSubtitle(arguments.getString(VodTvSectionFragment.AV_CONTENT_GROUP_BUNDLE));

    setBackgroundType(BG_LIGHT);
    mPlayerGlue.playWhenPrepared();
}
 
Example #28
Source File: DefaultMediaSourceBuilder.java    From ExoMedia with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public MediaSource build(@NonNull Context context, @NonNull Uri uri, @NonNull String userAgent, @NonNull Handler handler, @Nullable TransferListener transferListener) {
    DataSource.Factory dataSourceFactory = buildDataSourceFactory(context, userAgent, transferListener);

    return new ExtractorMediaSource.Factory(dataSourceFactory)
            .setExtractorsFactory(new DefaultExtractorsFactory())
            .createMediaSource(uri);
}
 
Example #29
Source File: VideoDetailsFragment.java    From Loop with Apache License 2.0 5 votes vote down vote up
private MediaSource getMediaSource(String videoUrl){
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
//        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "Loop"), bandwidthMeter);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(LoopApplication.getInstance().getApplicationContext(), Util.getUserAgent(LoopApplication.getInstance().getApplicationContext(), "Loop"), bandwidthMeter);
        // Produces Extractor instances for parsing the media data.
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // This is the MediaSource representing the media to be played.
        MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(videoUrl),
                dataSourceFactory, extractorsFactory, null, null);
        // Loops the video indefinitely.
//        LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);
        return mediaSource;
    }
 
Example #30
Source File: ExoMediaPlayer.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void load(@NonNull final Uri uri) {
    if (mediaSource != null) {
        mediaSource.releaseSource();
    }
    if (mediaPlayerListener != null) {
        mediaPlayerListener.onLoading();
    }
    mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);

    loadingMediaUri = uri;
    exoPlayer.prepare(mediaSource);
}