Exoplayer 是一个非常先进的库。即使写一个最低限度的代码也需要 40-50 行代码。所以如果你真的想用刀切洋葱,这里有一个直接复制的意大利面:
//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;
}
只需player view
在您的活动布局中添加 ,调用startPlayingVideo(...)
inonCreate()
和stopPlayer()
. onStop()
我不是专家,但是如果您愿意,我可以尝试解释一下,但是您没有要求任何复杂的东西,所以这里只是代码