Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting headers for streaming mp4 video and playing files with Exoplayer

Api has a token header that i need to set but the video is not encrypted. I have two questions: How can i play .mpg,.mpeg,.3gp,.mov and other files from disk with exoplayer? How can i set headers with exoplayer and stream mp4 video from url?

like image 760
ddog Avatar asked Jul 01 '15 13:07

ddog


2 Answers

Figured out the answer:

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);
like image 135
ddog Avatar answered Sep 28 '22 02:09

ddog


   // (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();
like image 26
M. H. Avatar answered Sep 28 '22 01:09

M. H.