如何使用android exoplayer

时间:2015-09-28 11:05:16

标签: android video-streaming exoplayer

我希望在我的应用中实施Google ExoPlayer。他们的文档对我来说似乎很模糊,而我正在寻找的是从URL播放视频,没有复杂的自定义设置或类似的东西。 Haven没有找到关于如何做的任何教程。他们在git上的例子对于我需要的东西来说太复杂了,因为我不熟悉视频流,所以我并不了解。我到目前为止所做的就是显示com.google.android.exoplayer.AspectRatioFrameLayout

基本上,我有一个URL。我需要播放视频,并在用户翻转屏幕时处理onConfigurationChanged

有人可以帮忙吗?

6 个答案:

答案 0 :(得分:25)

ExoMedia库将exoplayer包装在更简单的api中,并提供用于布局的视频视图。请参阅github上的用法示例:https://github.com/brianwernick/ExoMedia/

答案 1 :(得分:2)

如果您只想显示视频网址,VideoView会更好。 ExoPlayer需要一些开发工作,即使是调用它的简单实例也是如此。但是,有一个优点是更快,更有效的播放,由活跃的开源社区支持。 This link为实现提供了很好的理由,提供了切换到ExoPlayer的充分理由。 当然,检查official developer guide,更新版本有拆分模块,以便更简单的实施。

答案 2 :(得分:1)

OR

您可以看到此教程视频。

https://youtu.be/mVJmOCb8Erw

答案 3 :(得分:1)

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(...)中调用onCreate()并在stopPlayer()中调用onStop()即可。我不是专家,但是如果您愿意,我可以尝试解释一下,但是您没有要求任何复杂的内容,因此这里只是代码

答案 4 :(得分:1)

//Add dependency in manifest file 
    implementation 'com.google.android.exoplayer:exoplayer:2.7.3'


// Add exoplayer in your layout(xml) file 
 <RelativeLayout
        android:id="@+id/rl_video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/videoFullScreenPlayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000"
        app:controller_layout_id="@layout/exo_playback_control_view"
        app:player_layout_id="@layout/exo_simple_player_view"
        app:repeat_toggle_modes="none"
        app:show_timeout="45000"
        app:surface_type="texture_view"
        />
        <ProgressBar
            android:id="@+id/spinnerVideoDetails"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:indeterminate="true"/>
    </RelativeLayout>




// open video with below code

// initialise varible 
 PlayerView videoFullScreenPlayer;
    SimpleExoPlayer player;
    ProgressBar spinnerVideoDetails;


// find Id 
 videoFullScreenPlayer = findViewById(R.id.videoFullScreenPlayer);
        spinnerVideoDetails = findViewById(R.id.spinnerVideoDetails);

// open video method 
private void setUp() {
        initializePlayer();
        if (videoUrl == null) {
            return;
        }
        buildMediaSource(Uri.parse(videoUrl ));
    }
    private void initializePlayer() {
        if (player == null) {

            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
            TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
            TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
            // 1. Create a default TrackSelector
            DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(32*1024, 64*1024, 1024, 1024).createDefaultLoadControl();
            // 2. Create the player
            player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
            videoFullScreenPlayer.setPlayer(player);
        }
    }

    private void buildMediaSource(Uri mUri) {
        // Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(mUri);
        // Prepare the player with the source.
        player.prepare(videoSource);
        player.setPlayWhenReady(true);
        player.addListener(this);
    }

    private void releasePlayer() {
        if (player != null) {
            player.release();
            player = null;
        }
    }

    private void pausePlayer() {
        if (player != null) {
            player.setPlayWhenReady(false);
            player.getPlaybackState();
        }
    }

    private void resumePlayer() {
        if (player != null) {
            player.setPlayWhenReady(true);
            player.getPlaybackState();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        pausePlayer();
       /* if (mRunnable != null) {
            mHandler.removeCallbacks(mRunnable);
        }*/
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        resumePlayer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releasePlayer();
    }

    @Override
    public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
    }

    @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 Player.STATE_BUFFERING:
                spinnerVideoDetails.setVisibility(View.VISIBLE);
                break;
            case Player.STATE_ENDED:
                // Activate the force enable
                break;
            case Player.STATE_IDLE:
                break;
            case Player.STATE_READY:
                spinnerVideoDetails.setVisibility(View.GONE);
                break;
            default:
                // status = PlaybackStatus.IDLE;
                break;
        }
    }

    @Override
    public void onRepeatModeChanged(int repeatMode) {
    }

    @Override
    public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
    }

    @Override
    public void onPlayerError(ExoPlaybackException error) {
    }

    @Override
    public void onPositionDiscontinuity(int reason) {
    }

    @Override
    public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
    }

    @Override
    public void onSeekProcessed() {
    }

答案 5 :(得分:0)

这是一个基于ExoPlayer的名为{strong> MagicalExoPlayer 的新Github库。

支持 MP4 HLS Dash

支持自定义纵横比

支持全屏