谷歌 ExoPlayer 指南

Posted

技术标签:

【中文标题】谷歌 ExoPlayer 指南【英文标题】:Google ExoPlayer guide 【发布时间】:2017-06-10 10:31:42 【问题描述】:

我正在努力使用 ExoPlayer 构建基本应用程序。

您好,我对“入门”部分有疑问。不知道播放视频或流媒体需要使用什么。如何停止,播放,暂停......另一个问题是我不知道我在提供什么,例如,在 DefaultDataSourceFactory 构造函数中,为什么,我得到了什么和没有一些参数......我很困惑整个用法...请帮助!谢谢!

【问题讨论】:

请提及您在实施 ExoPlayer 时遇到的困难 您好,我对“入门”部分有疑问。不知道播放视频或流媒体需要使用什么。如何停止,播放,暂停......另一个问题是我不知道我提供了什么,例如,在DefaultDataSourceFactory 构造函数中,为什么,我得到了什么和没有一些参数......我很漂亮对整个用法感到困惑......请帮忙!谢谢! github.com/google/ExoPlayer ,从 git 下载演示应用程序,按原样导入。运行示例,探索代码...在运行应用程序时,如果您遇到任何问题,请随时发表评论...!! 【参考方案1】:

首先你需要在你的 build.gradle 中添加这个compile 'com.google.android.exoplayer:exoplayer:r2.1.1'

1.您的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    >
    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:focusable="true"
        android:layout_
        android:layout_/>
</RelativeLayout>

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=<your 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及以下版本上不起作用。

【讨论】:

完美,这正是我在网上寻找了一个多小时的内容。我正要放弃。此示例对视频和音频都适用。只需在清单中添加 是否可以将Exoplayer 与自定义视频表面一起使用而不是com.google.android.exoplayer2.ui.SimpleExoPlayerView 我的问题***.com/questions/48920678/…

以上是关于谷歌 ExoPlayer 指南的主要内容,如果未能解决你的问题,请参考以下文章

AndroidX Media3之ExoPlayer简单使用

Android视频播放器Exoplayer自定义

android,Exoplayer实现视频播放器

无法解决:com.google.android.exoplayer:exoplayer-smoothstreaming:2.6.1

如何在 android 中实现 Exoplayer 2.11.1?

Android 依赖 'com.google.android.exoplayer:exoplayer' 有不同版本的编译(r2.1.0)和运行时(2.9.1)类路径