Exoplayer v2,现场视频流

我想用ExoPlayer替换视频视图,以便在我的应用中进行直播。Exoplayer v2,现场视频流

我无法在ExoPlayer上找到任何示例,用videoView.setVideoURI("")替代我的代码以及实时视频的实现。

执行的任何人都可以请帮忙吗?

感谢

回答:

加波纹管编译到您的gradle产出:

//Video Playback library 

compile 'com.google.android.exoplayer:exoplayer:r2.5.+'

添加下面一行到你的XML文件:

<com.google.android.exoplayer2.ui.SimpleExoPlayerView 

android:id="@+id/video_player"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:resize_mode="fill"></com.google.android.exoplayer2.ui.SimpleExoPlayerView>

写celow代码在你的活动:

private SimpleExoPlayer player; 

private TrackSelector trackSelector;

// replace URL with your url/path

private String urlToPlay;

//I have used butterknife you can bind view as per your convinience

@BindView(R.id.video_player)

SimpleExoPlayerView feedVideo;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setUpVideo();

}

private void setUpVideo() {

// 1. Create a default TrackSelector

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

TrackSelection.Factory videoTrackSelectionFactory =

new AdaptiveTrackSelection.Factory(bandwidthMeter);

trackSelector = new

DefaultTrackSelector(videoTrackSelectionFactory);

// 2. Create the player

player = ExoPlayerFactory.newSimpleInstance(this,

trackSelector);

feedVideo.setPlayer(player);

setUpVideoData();

}

private void setUpVideoData() {

// Measures bandwidth during playback. Can be null if not required.

DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

// Produces DataSource instances through which media data is loaded.

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,

Util.getUserAgent(this, "yourApplicationName"), bandwidthMeter);

// Produces Extractor instances for parsing the media data.

ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

// This is the MediaSource representing the media to be played.

MediaSource videoSource = new ExtractorMediaSource(Uri.parse(urlToPlay),

dataSourceFactory, extractorsFactory, null, null);

// Prepare the player with the source.

player.prepare(videoSource);

// Auto Play video as soon as it buffers

player.setPlayWhenReady(true);

}

@Override

public void onPause() {

super.onPause();

pauseLivePreview();

}

@Override

public void onResume() {

super.onResume();

resumeLivePreview();

}

private void resumeLivePreview() {

if (player != null) {

player.setPlayWhenReady(true);

}

}

private void pauseLivePreview() {

if (player != null) {

if (feedVideo != null && feedVideo.getPlayer() != null) {

feedVideo.getPlayer().release();

}

}

Refrences https://github.com/google/ExoPlayer https://google.github.io/ExoPlayer/guide.html

以上是 Exoplayer v2,现场视频流 的全部内容, 来源链接: utcz.com/qa/264542.html

回到顶部