第一步配置gadle
implementation 'com.google.android.exoplayer:exoplayer-core:2.11.7'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.11.7'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.7'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.11.7'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.11.7'
implementation 'com.google.android.exoplayer:extension-rtmp:2.11.7'
第二步layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
android:id="@+id/video_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
>
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
<ProgressBar
android:id="@+id/mProgressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Widget.AppCompat.ProgressBar"
android:visibility="gone"
>
</ProgressBar>
</androidx.constraintlayout.widget.ConstraintLayout>
第三步activity
package com.example.exoplayetest; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.ProgressBar; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; public class MainActivity extends AppCompatActivity{ private SimpleExoPlayer exoPlayer; private AspectRatioFrameLayout videoFrame;//用来控制视频的宽高比 private SurfaceView surfaceView; //播放区 private ProgressBar mProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); surfaceView = (SurfaceView) findViewById(R.id.surface_view); videoFrame = (AspectRatioFrameLayout) findViewById(R.id.video_frame); mProgressBar=findViewById(R.id.mProgressBar); exoPlayer=new SimpleExoPlayer.Builder(getApplicationContext()).build(); exoPlayer.prepare(mHlsMediaSource.getMediaSource(this)); exoPlayer.addListener(new ExoPlayerrListener()); // videoFrame.setAspectRatio(1.9f); exoPlayer.setVideoSurface(surfaceView.getHolder().getSurface()); exoPlayer.setPlayWhenReady(true); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(hasFocus){ getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } } class ExoPlayerrListener implements Player.EventListener { @Override public void onTimelineChanged(Timeline timeline, int reason) { } @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState){ case 2://缓冲 mProgressBar.setVisibility(View.VISIBLE); break; case 3://就绪 mProgressBar.setVisibility(View.GONE); break; case 4://完成 break; } } } }
mediasource
package com.example.exoplayetest;
import android.content.Context;
import android.net.Uri;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.util.Util;
public class mHlsMediaSource {
public static HlsMediaSource getMediaSource(Context context){
Uri contentUri = Uri.parse(Constant.ccty1);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getPackageName()));
CacheDataSourceFactory cacheDataSourceFactory=new CacheDataSourceFactory(new SimpleCache(context.getFilesDir(),new NoOpCacheEvictor(),new ExoDatabaseProvider(context)),dataSourceFactory);
com.google.android.exoplayer2.source.hls.HlsMediaSource.Factory factory=new com.google.android.exoplayer2.source.hls.HlsMediaSource.Factory(cacheDataSourceFactory);
return factory.createMediaSource(contentUri);
}
}
constant
public class Constant {
public static String ccty1="http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
public static String ccty3="http://ivi.bupt.edu.cn/hls/cctv2hd.m3u8";
public static String ccty5="http://ivi.bupt.edu.cn/hls/cctv5hd.m3u8";
public static String ccty5_="http://ivi.bupt.edu.cn/hls/cctv5phd.m3u8";
public static String ccty6="http://ivi.bupt.edu.cn/hls/cctv6hd.m3u8";
public static String RTMP ="rtsp://192.168.1.11/test.hls";
}