6

Api 有一个我需要设置的令牌标头,但视频未加密。我有两个问题:如何使用 exoplayer 从磁盘播放 .mpg、.mpeg、.3gp、.mov 和其他文件?如何使用 exoplayer 设置标题并从 url 流式传输 mp4 视频?

4

5 回答 5

6

想出了答案:

DefaultHttpDataSource source = new DefaultHttpDataSource(userAgent, null);
    source.setRequestProperty("Authorization", "your auth code");
    source.setRequestProperty("Accept", "...");

    ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, source, extractor, 2,
            BUFFER_SIZE);
    MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource,
            null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING, 5000, null, player.getMainHandler(),
            player, 50);
    MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,
            null, true, player.getMainHandler(), player);
于 2015-07-03T09:45:54.813 回答
3
   // (1) Create method returns  'DataSource.Factory'

     public DataSource.Factory headers() {
            Map<String, String> headersMap = new HashMap<>();
            headersMap.put("iid", "aaa123 ");
            headersMap.put("version", "1.4");
            headersMap.put("agent", "phone");
            headersMap.put("token", "dfdf4f4yt5yf5fh4f5");
            return new DefaultHttpDataSource.Factory().setDefaultRequestProperties(headersMap);
        }
    
    
    
    // (2) Add headers() method call to the player

           SimpleExoPlayer player = new SimpleExoPlayer.Builder(context)
                    .setMediaSourceFactory(new 
                     DefaultMediaSourceFactory(headers()))
                    .build();
于 2021-05-11T10:35:12.637 回答
2

在 exoplayer 2.13.2 上,不推荐使用 DefaultDataSourceFactory 构造方法,您可以改用 Factory。

val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
                .setUserAgent("")
                .setDefaultRequestProperties(hashMapOf("" to ""))
                .setDefaultRequestProperties(hashMapOf("" to ""))
            val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(MediaItem.fromUri(url))
            val player = SimpleExoPlayer
                .Builder(this)
                .build()
            binding.videoPlayerView.player = player
            player.setMediaSource(mediaSource)
            player.prepare()
            player.play()

于 2021-03-08T15:30:58.447 回答
1
  // 1. Create a default TrackSelector
    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(this, trackSelector,  
   loadControl);
    simpleExoPlayerView = new SimpleExoPlayerView(this);
    simpleExoPlayerView = (SimpleExoPlayerView)
  findViewById(R.id.player_view);

  //Set media controller
    simpleExoPlayerView.setUseController(true);
    simpleExoPlayerView.requestFocus();

   // Bind the player to the view.
    simpleExoPlayerView.setPlayer(player); 

   String username = "username";
    String password = "password";

    // encrypt Authdata
    byte[] toEncrypt = (username + ":" + password).getBytes();
    encoded = Base64.encodeToString(toEncrypt, Base64.DEFAULT);
    DefaultHttpDataSourceFactory dataSourceFactory = new 
    DefaultHttpDataSourceFactory(Util.getUserAgent(this,   
    "exoplayer2example"));

    DefaultHttpDataSource source = new  DefaultHttpDataSource(Util.getUserAgent(this, "exoplayer2example"),null);
    dataSourceFactory.setDefaultRequestProperty("Authorization","Basic "+encoded);
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    MediaSource videoSource = new ExtractorMediaSource(Uri.parse("https://example.com/assets/video/SampleVideo.mp4"),
            dataSourceFactory, extractorsFactory, null, null);
    final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);
    exoplayer.prepare(videoSource);
于 2018-02-23T08:48:38.967 回答
0

如何使用 exoplayer 设置标题并从 url 流式传输 mp4 视频?

我实施了基本授权,所以:

private SimpleExoPlayer player;
private PlayerView playerView;

private void initializePlayer() {
    player = ExoPlayerFactory.newSimpleInstance(
            new DefaultRenderersFactory(this),
            new DefaultTrackSelector(),
            new DefaultLoadControl());
    playerView.setPlayer(player);
    player.setPlayWhenReady(true);
    player.seekTo(0, 0);

    Uri uri = Uri.parse(getString(R.string.media_url));
    MediaSource mediaSource = buildMediaSource(uri);
    player.prepare(mediaSource, true, false);
}

private MediaSource buildMediaSource(final Uri uri) {
    HttpDataSource.BaseFactory myDSFactory = new HttpDataSource.BaseFactory() {
        @Override
        protected HttpDataSource createDataSourceInternal(HttpDataSource.RequestProperties defaultRequestProperties) {
            byte[] toEncrypt = uri.getUserInfo().getBytes();
            String encoded = "Basic " + Base64.encodeToString(toEncrypt, Base64.DEFAULT).trim();
            DefaultHttpDataSourceFactory dsf = new DefaultHttpDataSourceFactory("exoplayer-codelab");
            HttpDataSource ds = dsf.createDataSource();
            ds.setRequestProperty("Authorization", encoded);
            return ds;
        }
    };

    ExtractorMediaSource.Factory emf = new ExtractorMediaSource.Factory(myDSFactory);
    return emf.createMediaSource(uri);
}
于 2019-03-25T09:35:32.387 回答