如何使用ExtractorMediaSource.Factory在ExoPlayer中设置自定义标题?

时间:2018-06-21 14:21:07

标签: java android video exoplayer

我很难将Header发送到服务器,以便获得观看视频的许可。我已经编写的重要代码如下:

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

SimpleExoPlayer player =
   ExoPlayerFactory.newSimpleInstance(getApplicationContext(),  trackSelector);

PlayerView playerView = new PlayerView(this);
playerView.setLayoutParams(new FrameLayout.LayoutParams(contentWidth, contentWidth / 16 * 9));
playerView.setPlayer(player);
DefaultBandwidthMeter bandwidthmeter = new DefaultBandwidthMeter();
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
                        Util.getUserAgent(getApplicationContext(), "yourApplicationName"), bandwidthmeter);
 MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(streamUrl)
                        ;
 player.prepare(videoSource);

如果有人有答案,请帮我解决!

2 个答案:

答案 0 :(得分:1)

您可以点这个

HlsMediaSource hlsMediaSource = null;
// 1. Create a default TrackSelector
    Handler mainHandler = new Handler();
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory = new 
    AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new 
    DefaultTrackSelector(videoTrackSelectionFactory);

    // 2. Create a default LoadControl
    LoadControl loadControl = new DefaultLoadControl();

    // 3. Create the player
    player = ExoPlayerFactory.newSimpleInstance(MainActivityMob.getContext(), trackSelector, loadControl);

    // Measures bandwidth during playback. Can be null if not required.
    DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
    // Produces DataSource instances through which media data is loaded.
    DefaultHttpDataSource.Factory MGSource = new DefaultHttpDataSourceFactory(Util.getUserAgent( MainActivityMob.getContext(), "myExoplayer"), null);
    MGSource.getDefaultRequestProperties().set("YOUR KEY", "YOUR VALUE");

    // This is the MediaSource representing the media to be played. new AdaptiveMediaSourceEventListener()
    hlsMediaSource = new HlsMediaSource(Uri.parse(urlCH), MGSource, mainHandler, new AdaptiveMediaSourceEventListener() {


        @Override
        public void onLoadStarted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs) {

        }

        @Override
        public void onLoadCompleted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {
 progressBar.setVisibility(View.INVISIBLE);

        }

        @Override
        public void onLoadCanceled(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {

        }

        @Override
        public void onLoadError(DataSpec dataSpec, int dataType, int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded, IOException error, boolean wasCanceled) {

        }

        @Override
        public void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs) {

        }

        @Override
        public void onDownstreamFormatChanged(int trackType, Format trackFormat, int trackSelectionReason, Object trackSelectionData, long mediaTimeMs) {

        }

    });

答案 1 :(得分:0)

DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(getApplicationContext(), "yourApplicationName"), null);

httpDataSourceFactory.defaultRequestProperties.set("Key", "Value"); // or maps

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), null, httpDataSourceFactory);

MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                          .createMediaSource(streamUrl);