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

The following examples show how to use com.google.android.exoplayer2.source.ProgressiveMediaSource. 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: DownloadHelper.java    From Telegram-FOSS with GNU General Public License v2.0 7 votes vote down vote up
/**
 * Utility method to create a MediaSource which only contains the tracks defined in {@code
 * downloadRequest}.
 *
 * @param downloadRequest A {@link DownloadRequest}.
 * @param dataSourceFactory A factory for {@link DataSource}s to read the media.
 * @return A MediaSource which only contains the tracks defined in {@code downloadRequest}.
 */
public static MediaSource createMediaSource(
    DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory) {
  MediaSourceFactory factory;
  switch (downloadRequest.type) {
    case DownloadRequest.TYPE_DASH:
      factory = DASH_FACTORY;
      break;
    case DownloadRequest.TYPE_SS:
      factory = SS_FACTORY;
      break;
    case DownloadRequest.TYPE_HLS:
      factory = HLS_FACTORY;
      break;
    case DownloadRequest.TYPE_PROGRESSIVE:
      return new ProgressiveMediaSource.Factory(dataSourceFactory)
          .createMediaSource(downloadRequest.uri);
    default:
      throw new IllegalStateException("Unsupported type: " + downloadRequest.type);
  }
  return factory.createMediaSource(
      downloadRequest.uri, dataSourceFactory, downloadRequest.streamKeys);
}
 
Example #2
Source File: ExoPlayerHelper.java    From ExoPlayer-Wrapper with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri) {
    int type = Util.inferContentType(uri);
    switch (type) {
        case C.TYPE_SS:
            return new SsMediaSource.Factory(mDataSourceFactory).createMediaSource(uri);
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(mDataSourceFactory).createMediaSource(uri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(mDataSourceFactory).createMediaSource(uri);
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(mDataSourceFactory).createMediaSource(uri);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #3
Source File: DownloadHelper.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Utility method to create a MediaSource which only contains the tracks defined in {@code
 * downloadRequest}.
 *
 * @param downloadRequest A {@link DownloadRequest}.
 * @param dataSourceFactory A factory for {@link DataSource}s to read the media.
 * @return A MediaSource which only contains the tracks defined in {@code downloadRequest}.
 */
public static MediaSource createMediaSource(
    DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory) {
  MediaSourceFactory factory;
  switch (downloadRequest.type) {
    case DownloadRequest.TYPE_DASH:
      factory = DASH_FACTORY;
      break;
    case DownloadRequest.TYPE_SS:
      factory = SS_FACTORY;
      break;
    case DownloadRequest.TYPE_HLS:
      factory = HLS_FACTORY;
      break;
    case DownloadRequest.TYPE_PROGRESSIVE:
      return new ProgressiveMediaSource.Factory(dataSourceFactory)
          .createMediaSource(downloadRequest.uri);
    default:
      throw new IllegalStateException("Unsupported type: " + downloadRequest.type);
  }
  return factory.createMediaSource(
      downloadRequest.uri, dataSourceFactory, downloadRequest.streamKeys);
}
 
Example #4
Source File: ExoPlayerView.java    From AerialDream with GNU General Public License v3.0 6 votes vote down vote up
public void setUri(Uri uri) {
    if (uri == null) {
        return;
    }

    player.stop();
    prepared = false;

    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory("Aerial Dream");
    DataSource.Factory dataSourceFactory = cacheSize > 0
            ? new CacheDataSourceFactory(new SimpleCache(getContext().getCacheDir(), new LeastRecentlyUsedCacheEvictor(cacheSize), new ExoDatabaseProvider(getContext())), httpDataSourceFactory, 0)
            : httpDataSourceFactory;

    mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
    player.prepare(mediaSource);
}
 
Example #5
Source File: MediaSourceCreator.java    From ExoVideoView with Apache License 2.0 6 votes vote down vote up
public MediaSource buildMediaSource(Uri uri, String overrideExtension) {
    @C.ContentType int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
            : Util.inferContentType("." + overrideExtension);
    switch (type) {
        case C.TYPE_SS:
            SsMediaSource.Factory factory = new SsMediaSource.Factory(buildDataSourceFactory(false));
            factory.createMediaSource(uri);
            return factory.createMediaSource(uri);
        case C.TYPE_DASH:
            DashMediaSource.Factory factory1 = new DashMediaSource.Factory(buildDataSourceFactory(false));
            return factory1.createMediaSource(uri);
        case C.TYPE_HLS:
            HlsMediaSource.Factory factory2 = new HlsMediaSource.Factory(buildDataSourceFactory(false));
            return factory2.createMediaSource(uri);
        case C.TYPE_OTHER:
            ProgressiveMediaSource.Factory factory3 = new ProgressiveMediaSource.Factory(buildDataSourceFactory(false));
            return factory3.createMediaSource(uri);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #6
Source File: ExoPlayerMediaSourceBuilder.java    From PreviewSeekBar with Apache License 2.0 6 votes vote down vote up
public MediaSource getMediaSource(boolean preview) {
    switch (streamType) {
        case C.TYPE_SS:
            return new SsMediaSource.Factory(new DefaultDataSourceFactory(context, null,
                    getHttpDataSourceFactory(preview))).createMediaSource(uri);
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(new DefaultDataSourceFactory(context, null,
                    getHttpDataSourceFactory(preview))).createMediaSource(uri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(getDataSourceFactory(preview)).createMediaSource(uri);
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(getDataSourceFactory(preview)).createMediaSource(uri);
        default: {
            throw new IllegalStateException("Unsupported type: " + streamType);
        }
    }
}
 
Example #7
Source File: PlayerActivity.java    From exoplayer-intro with Apache License 2.0 6 votes vote down vote up
private MediaSource buildMediaSource(Uri uri) {
  // These factories are used to construct two media sources below
  DataSource.Factory dataSourceFactory =
          new DefaultDataSourceFactory(this, "exoplayer-codelab");
  ProgressiveMediaSource.Factory mediaSourceFactory =
          new ProgressiveMediaSource.Factory(dataSourceFactory);

  // Create a media source using the supplied URI
  MediaSource mediaSource1 = mediaSourceFactory.createMediaSource(uri);

  // Additionally create a media source using an MP3
  Uri audioUri = Uri.parse(getString(R.string.media_url_mp3));
  MediaSource mediaSource2 = mediaSourceFactory.createMediaSource(audioUri);

  return new ConcatenatingMediaSource(mediaSource1, mediaSource2);
}
 
Example #8
Source File: PlayerTextureView.java    From Mp4Composer-android with MIT License 6 votes vote down vote up
public PlayerTextureView(Context context, String path) {
    super(context, null, 0);

    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "yourApplicationName"));

    // This is the MediaSource representing the media to be played.
    MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(Uri.parse(path));

    LoopingMediaSource loopingMediaSource = new LoopingMediaSource(videoSource);


    // SimpleExoPlayer
    player = ExoPlayerFactory.newSimpleInstance(context);
    // Prepare the player with the source.
    player.prepare(loopingMediaSource);
    player.addVideoListener(this);

    setSurfaceTextureListener(this);
}
 
Example #9
Source File: ExoPlayerImpl.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private MediaSource createMediaSource(Uri uri, String extension) {
    int type = Util.inferContentType(uri, extension);
    DataSource.Factory dataSourceFactory = buildDataSourceFactory();
    switch (type) {
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(uri);
        case C.TYPE_SS:
            return new SsMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(uri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(uri);
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(uri);
        default:
            throw new IllegalStateException("Unsupported type: " + type);
    }
}
 
Example #10
Source File: DownloadHelper.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to create a MediaSource which only contains the tracks defined in {@code
 * downloadRequest}.
 *
 * @param downloadRequest A {@link DownloadRequest}.
 * @param dataSourceFactory A factory for {@link DataSource}s to read the media.
 * @return A MediaSource which only contains the tracks defined in {@code downloadRequest}.
 */
public static MediaSource createMediaSource(
    DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory) {
  @Nullable Constructor<? extends MediaSourceFactory> constructor;
  switch (downloadRequest.type) {
    case DownloadRequest.TYPE_DASH:
      constructor = DASH_FACTORY_CONSTRUCTOR;
      break;
    case DownloadRequest.TYPE_SS:
      constructor = SS_FACTORY_CONSTRUCTOR;
      break;
    case DownloadRequest.TYPE_HLS:
      constructor = HLS_FACTORY_CONSTRUCTOR;
      break;
    case DownloadRequest.TYPE_PROGRESSIVE:
      return new ProgressiveMediaSource.Factory(dataSourceFactory)
          .createMediaSource(downloadRequest.uri);
    default:
      throw new IllegalStateException("Unsupported type: " + downloadRequest.type);
  }
  return createMediaSourceInternal(
      constructor, downloadRequest.uri, dataSourceFactory, downloadRequest.streamKeys);
}
 
Example #11
Source File: ExoMediaSourceHelper.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
public MediaSource getMediaSource(String uri, Map<String, String> headers, boolean isCache) {
    Uri contentUri = Uri.parse(uri);
    if ("rtmp".equals(contentUri.getScheme())) {
        return new ProgressiveMediaSource.Factory(new RtmpDataSourceFactory(null))
                .createMediaSource(contentUri);
    }
    int contentType = inferContentType(uri);
    DataSource.Factory factory;
    if (isCache) {
        factory = getCacheDataSourceFactory();
    } else {
        factory = getDataSourceFactory();
    }
    if (mHttpDataSourceFactory != null) {
        setHeaders(headers);
    }
    switch (contentType) {
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(factory).createMediaSource(contentUri);
        case C.TYPE_SS:
            return new SsMediaSource.Factory(factory).createMediaSource(contentUri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(factory).createMediaSource(contentUri);
        default:
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(factory).createMediaSource(contentUri);
    }
}
 
Example #12
Source File: Alarmio.java    From Alarmio with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    DebugUtils.setup(this);

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    listeners = new ArrayList<>();
    alarms = new ArrayList<>();
    timers = new ArrayList<>();

    player = new SimpleExoPlayer.Builder(this).build();
    player.addListener(this);

    DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), null);
    hlsMediaSourceFactory = new HlsMediaSource.Factory(dataSourceFactory);
    progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);

    int alarmLength = PreferenceData.ALARM_LENGTH.getValue(this);
    for (int id = 0; id < alarmLength; id++) {
        alarms.add(new AlarmData(id, this));
    }

    int timerLength = PreferenceData.TIMER_LENGTH.getValue(this);
    for (int id = 0; id < timerLength; id++) {
        TimerData timer = new TimerData(id, this);
        if (timer.isSet())
            timers.add(timer);
    }

    if (timerLength > 0)
        startService(new Intent(this, TimerService.class));

    SleepReminderService.refreshSleepTime(this);
}
 
Example #13
Source File: AdsMediaSource.java    From MediaSDK with Apache License 2.0 5 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 ProgressiveMediaSource}.
 *
 * @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 adViewProvider Provider of views for the ad UI.
 */
public AdsMediaSource(
    MediaSource contentMediaSource,
    DataSource.Factory dataSourceFactory,
    AdsLoader adsLoader,
    AdsLoader.AdViewProvider adViewProvider) {
  this(
      contentMediaSource,
      new ProgressiveMediaSource.Factory(dataSourceFactory),
      adsLoader,
      adViewProvider);
}
 
Example #14
Source File: LocalPlayback.java    From klingar with Apache License 2.0 5 votes vote down vote up
LocalPlayback(Context context, MusicController musicController, AudioManager audioManager,
              WifiManager wifiManager, Call.Factory callFactory) {
  this.context = context;
  this.musicController = musicController;
  this.audioManager = audioManager;
  this.wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "klingar");
  String agent = Util.getUserAgent(context, context.getResources().getString(R.string.app_name));
  this.mediaSourceFactory = new ProgressiveMediaSource.Factory(new DefaultDataSourceFactory(
      context, null, new OkHttpDataSourceFactory(callFactory, agent)));
}
 
Example #15
Source File: ReactExoplayerView.java    From react-native-video with MIT License 5 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.Factory(
                    new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
                    buildDataSourceFactory(false)
            ).setLoadErrorHandlingPolicy(
                    config.buildLoadErrorHandlingPolicy(minLoadRetryCount)
            ).createMediaSource(uri);
        case C.TYPE_DASH:
            return new DashMediaSource.Factory(
                    new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
                    buildDataSourceFactory(false)
            ).setLoadErrorHandlingPolicy(
                    config.buildLoadErrorHandlingPolicy(minLoadRetryCount)
            ).createMediaSource(uri);
        case C.TYPE_HLS:
            return new HlsMediaSource.Factory(
                    mediaDataSourceFactory
            ).setLoadErrorHandlingPolicy(
                    config.buildLoadErrorHandlingPolicy(minLoadRetryCount)
            ).createMediaSource(uri);
        case C.TYPE_OTHER:
            return new ProgressiveMediaSource.Factory(
                    mediaDataSourceFactory
            ).setLoadErrorHandlingPolicy(
                    config.buildLoadErrorHandlingPolicy(minLoadRetryCount)
            ).createMediaSource(uri);
        default: {
            throw new IllegalStateException("Unsupported type: " + type);
        }
    }
}
 
Example #16
Source File: ViewVideoActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) {
    progressBar.setVisibility(View.VISIBLE);
    FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(retrofit, gfycatId,
            new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() {
                @Override
                public void success(String webm, String mp4) {
                    progressBar.setVisibility(View.GONE);
                    mVideoUri = Uri.parse(webm);
                    videoDownloadUrl = mp4;
                    dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this,
                            Util.getUserAgent(ViewVideoActivity.this, "Infinity"));
                    player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
                    preparePlayer(savedInstanceState);
                }

                @Override
                public void failed(int errorCode) {
                    progressBar.setVisibility(View.GONE);
                    if (videoType == VIDEO_TYPE_GFYCAT) {
                        if (errorCode == 404 && needErrorHandling) {
                            if (mSharedPreferences.getBoolean(SharedPreferencesUtils.AUTOMATICALLY_TRY_REDGIFS, true)) {
                                loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false);
                            } else {
                                Snackbar.make(coordinatorLayout, R.string.load_video_in_redgifs, Snackbar.LENGTH_INDEFINITE).setAction(R.string.yes,
                                        view -> loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false)).show();
                            }
                        } else {
                            Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(ViewVideoActivity.this, R.string.fetch_redgifs_video_failed, Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
 
Example #17
Source File: AdsMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 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 ProgressiveMediaSource}.
 *
 * @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 adViewProvider Provider of views for the ad UI.
 */
public AdsMediaSource(
    MediaSource contentMediaSource,
    DataSource.Factory dataSourceFactory,
    AdsLoader adsLoader,
    AdsLoader.AdViewProvider adViewProvider) {
  this(
      contentMediaSource,
      new ProgressiveMediaSource.Factory(dataSourceFactory),
      adsLoader,
      adViewProvider);
}
 
Example #18
Source File: MyActivity.java    From googleads-ima-android with Apache License 2.0 5 votes vote down vote up
private void initializePlayer() {
  // Create a SimpleExoPlayer and set is as the player for content and ads.
  player = new SimpleExoPlayer.Builder(this).build();
  playerView.setPlayer(player);
  adsLoader.setPlayer(player);

  DataSource.Factory dataSourceFactory =
      new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)));

  ProgressiveMediaSource.Factory mediaSourceFactory =
      new ProgressiveMediaSource.Factory(dataSourceFactory);

  // Create the MediaSource for the content you wish to play.
  MediaSource mediaSource =
      mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url)));

  // Create the AdsMediaSource using the AdsLoader and the MediaSource.
  AdsMediaSource adsMediaSource =
      new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView);

  // Prepare the content and ad to be played with the SimpleExoPlayer.
  player.prepare(adsMediaSource);

  // Set PlayWhenReady. If true, content and ads will autoplay.
  player.setPlayWhenReady(false);
}
 
Example #19
Source File: AdsMediaSource.java    From Telegram with GNU General Public License v2.0 5 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 ProgressiveMediaSource}.
 *
 * @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 adViewProvider Provider of views for the ad UI.
 */
public AdsMediaSource(
    MediaSource contentMediaSource,
    DataSource.Factory dataSourceFactory,
    AdsLoader adsLoader,
    AdsLoader.AdViewProvider adViewProvider) {
  this(
      contentMediaSource,
      new ProgressiveMediaSource.Factory(dataSourceFactory),
      adsLoader,
      adViewProvider);
}
 
Example #20
Source File: PlayerActivity.java    From exoplayer-intro with Apache License 2.0 4 votes vote down vote up
private MediaSource buildMediaSource(Uri uri) {
  DataSource.Factory dataSourceFactory =
          new DefaultDataSourceFactory(this, "exoplayer-codelab");
  return new ProgressiveMediaSource.Factory(dataSourceFactory)
          .createMediaSource(uri);
}
 
Example #21
Source File: MediaManager.java    From jellyfin-androidtv with GNU General Public License v2.0 4 votes vote down vote up
private static void playInternal(final BaseItemDto item, final int pos) {
    if (!ensureInitialized()) return;
    ensureAudioFocus();
    final ApiClient apiClient = TvApp.getApplication().getApiClient();
    AudioOptions options = new AudioOptions();
    options.setDeviceId(apiClient.getDeviceId());
    options.setItemId(item.getId());
    options.setMaxBitrate(TvApp.getApplication().getAutoBitrate());
    options.setMediaSources(item.getMediaSources());
    DeviceProfile profile = ProfileHelper.getBaseProfile(false);
    if (DeviceUtils.is60()) {
        ProfileHelper.setExoOptions(profile, false, true);
    } else {
        ProfileHelper.setVlcOptions(profile, false);
    }
    options.setProfile(profile);
    TvApp.getApplication().getPlaybackManager().getAudioStreamInfo(apiClient.getServerInfo().getId(), options, item.getResumePositionTicks(), false, apiClient, new Response<StreamInfo>() {
        @Override
        public void onResponse(StreamInfo response) {
            mCurrentAudioItem = item;
            mCurrentAudioStreamInfo = response;
            mCurrentAudioQueuePosition = pos;
            mCurrentAudioPosition = 0;
            if (nativeMode) {
                DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(TvApp.getApplication(), "ATV/ExoPlayer");

                mExoPlayer.setPlayWhenReady(true);
                mExoPlayer.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(response.ToUrl(apiClient.getApiUrl(), apiClient.getAccessToken()))));
            } else {
                Timber.i("Playback attempt via VLC of %s", response.getMediaUrl());
                Media media = new Media(mLibVLC, Uri.parse(response.getMediaUrl()));
                media.parse();
                mVlcPlayer.setMedia(media);

                media.release();
                mVlcPlayer.play();

            }
            if (mCurrentAudioQueuePosition == 0) {
                //we just started or repeated - re-create managed queue
                createManagedAudioQueue();
            }

            updateCurrentAudioItemPlaying(true);
            TvApp.getApplication().dataRefreshService.setLastMusicPlayback(System.currentTimeMillis());

            ReportingHelper.reportStart(item, mCurrentAudioPosition * 10000);
            for (AudioEventListener listener : mAudioEventListeners) {
                Timber.i("Firing playback state change listener for item start. %s", mCurrentAudioItem.getName());
                listener.onPlaybackStateChange(PlaybackController.PlaybackState.PLAYING, mCurrentAudioItem);
            }
        }

        @Override
        public void onError(Exception exception) {
            Utils.showToast(TvApp.getApplication(), "Unable to play audio " + exception.getLocalizedMessage());
        }
    });

}
 
Example #22
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;
}