问题
I am looking to implement Google's ExoPlayer
in my app. Their documentation seems pretty vague to me, and all I am looking for is to play a video from an URL, no complicated custom settings or anything like that. Haven't found any tutorials on how to do it. The example they have on git is way too complicated for what I need and, since I am new to video streaming, I did not understand much. All I have managed to do so far is to display a com.google.android.exoplayer.AspectRatioFrameLayout
.
Basically, I have an URL. I need to play the video, and handle onConfigurationChanged
when the user flips the screen.
Can anyone help?
回答1:
The ExoMedia library wraps exoplayer in simpler api and provides a video view for use in layouts. See usage examples on github: https://github.com/brianwernick/ExoMedia/
回答2:
A VideoView would be a better idea in case you wish to display only a Video URL. ExoPlayer requires some developmental effort, even for invoking its simple instance. However, there is an advantage of faster and more efficient playback, backed up by an active open-source community. This link provides a good walk through the implementation giving ample reasons to switch to ExoPlayer. Ofcourse, do checkout the official developer guide, the updated version has split modules for simpler implementation.
回答3:
OR
You can see this tutorial video.
https://youtu.be/mVJmOCb8Erw
回答4:
Exoplayer is a very advanced library. Even writing a bare minimum would take 40-50 lines of code. So if you really wanna use a sword to chop onions, here is a direct copy pasta :
//manifest.xml
<manifest ...>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
//app/build.gradle
apply plugin: 'com.android.application'
android {
...
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
...
implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
}
protected void onCreate(Bundle savedInstanceState) {
...
Context ctx =this;
String CONTENT_URL = "https://www.radiantmediaplayer.com/media/bbb-360p.mp4";
int playerID=R.id.pv_main;
int appNameStringRes = R.string.app_name;
startPlayingVideo(this,CONTENT_URL,playerID,appNameStringRes);
}
//
private void startPlayingVideo(Context ctx , String CONTENT_URL, int playerID, String appNameRes) {
PlayerView pvMain = ctx.findViewById(playerID);
//BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
//TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
//TrackSelector trackSelectorDef = new DefaultTrackSelector(videoTrackSelectionFactory);
TrackSelector trackSelectorDef = new DefaultTrackSelector();
SimpleExoPlayer absPlayerInternal = ExoPlayerFactory.newSimpleInstance(ctx, trackSelectorDef);
String userAgent = Util.getUserAgent(ctx, ctx.getString(appNameRes));
DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(ctx,userAgent);
Uri uriOfContentUrl = Uri.parse(CONTENT_URL);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);
absPlayerInternal.prepare(mediaSource);
absPlayerInternal.setPlayWhenReady(true);
pvMain.setPlayer(absPlayerInternal);
}
private void stopPlayer(PlayerView pv,SimpleExoPlayer absPlayer){
pv.setPlayer(null);
absPlayer.release();
absPlayer = null;
}
simply add the player view
in your activity layout, call the startPlayingVideo(...)
in onCreate()
and stopPlayer()
in the onStop()
. I am not an expert but i can try explaining this if you want, but you asked for no complicated stuff, so here is just the code
回答5:
Here is a new Github Library named MagicalExoPlayer, That based on ExoPlayer.
Supports MP4, HLS and Dash.
Supports custom Aspect Ratios
Support FullScreen
来源:https://stackoverflow.com/questions/32821472/how-to-use-android-exoplayer