我正在努力用ExoPlayer构建基本应用程序.
首先,您需要在build.gradle 编译'com.google.android.exoplayer:exoplayer:r2.1.1'中添加它.
1.您的布局文件
2.你的班级档案
public class ExoPlayerActivity extends AppCompatActivity implements ExoPlayer.EventListener { SimpleExoPlayer player; String path; // it can be url of video for online streaming or a url of local video @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exoplayer); // set your path here path=; // 1. Create a default TrackSelector BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); // 2. Create a default LoadControl LoadControl loadControl = new DefaultLoadControl(); // 3. Create the player player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); SimpleExoPlayerView playerView = (SimpleExoPlayerView) findViewById(R.id.player_view); playerView.setPlayer(player); playerView.setKeepScreenOn(true); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "ExoPlayer")); // 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(path), dataSourceFactory, extractorsFactory, null, null); // Prepare the player with the source. player.addListener(this); player.prepare(videoSource); playerView.requestFocus(); player.setPlayWhenReady(true); // to play video when ready. Use false to pause a video } @Override protected void onPause() { super.onPause(); if (player != null) { player.setPlayWhenReady(false); //to pause a video because now our video player is not in focus } } @Override public void onTimelineChanged(Timeline timeline, Object manifest) { } @Override public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { } @Override public void onLoadingChanged(boolean isLoading) { } @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_BUFFERING: //You can use progress dialog to show user that video is preparing or buffering so please wait break; case ExoPlayer.STATE_IDLE: //idle state break; case ExoPlayer.STATE_READY: // dismiss your dialog here because our video is ready to play now break; case ExoPlayer.STATE_ENDED: // do your processing after ending of video break; } } @Override public void onPlayerError(ExoPlaybackException error) { // show user that something went wrong. I am showing dialog but you can use your way AlertDialog.Builder adb = new AlertDialog.Builder(ExoPlayerActivity.this); adb.setTitle("Could not able to stream video"); adb.setMessage("It seems that something is going wrong.\nPlease try again."); adb.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); // take out user from this activity. you can skip this } }); AlertDialog ad = adb.create(); ad.show(); } @Override public void onPositionDiscontinuity() { //Video is not streaming properly Log.d("Mayur", "Discontinuity"); } @Override protected void onDestroy() { super.onDestroy(); player.release(); //it is important to release a player } }
我认为这对初学者来说已经足够了.另请注意,此库的标准音频和视频组件依赖于Android的MediaCodec API,该API是在Android 4.1(API级别16)中发布的.因此它不适用于Android 4.0及更低版本.