How to use android exoplayer
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/
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.gradleapply 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
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.