Api有一个我需要设置的令牌标头,但视频没有加密。 我有两个问题: 如何使用exoplayer从磁盘播放.mpg,.mpeg,.3gp,.mov和其他文件? 如何使用exoplayer设置标头并从网址流式传输mp4视频?
答案 0 :(得分:3)
找出答案:
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);
答案 1 :(得分: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);
答案 2 :(得分:1)
// (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();
答案 3 :(得分:0)
如何设置exoplayer标头并从网址流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);
}
答案 4 :(得分:0)
在 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()