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?
Setting headers for streaming mp4 video and playing files with Exoplayer
// (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();
Application is crashing var headersMap = HashMap<String,String>() headersMap.put("clienttoken", client_token) var dataSourceFactory = DefaultHttpDataSourceFactory(Util.getUserAgent(this, "POC")) dataSourceFactory.setDefaultRequestProperties(headersMap) exoPlayer = SimpleExoPlayer .Builder(this).setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)).build() –
Mylo
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);
ON exoplayer 2.13.2, DefaultDataSourceFactory constructor method is deprecated, you can use Factory instead.
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()
// 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);
setDefaultRequestProperty
is deprecated for DefaultHttpDataSourceFactory
–
Horus @Horus according to this issue you can use
HttpDataSource.Factory.getDefaultRequestProperties
and edit them. –
Syneresis How can i set headers with exoplayer and stream mp4 video from url?
I implemented basic authorisation so:
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);
}
Exoplayer:2.19.1' In, My case i had a id and i create a hardCoded url based on ID. The url needed bearer Token and then, return the video.
Here userAgent is compulsory. The answer by Jimmy was working.
Error : If you don't mentioned UserAgent
Unexpected exception loading stream java.lang.IllegalArgumentException: name is empty
val httpUrl = RestAdapter.getVideoUrl(id)
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
.setUserAgent("Android")
.setDefaultRequestProperties(hashMapOf("Authorization" to "Bearer "+Prefs.retrieveToken(this)))
val mediaSource: MediaSource =
ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(
MediaItem.fromUri(httpUrl)
)
© 2022 - 2025 — McMap. All rights reserved.