Exoplayer v2,实时视频流

Posted

技术标签:

【中文标题】Exoplayer v2,实时视频流【英文标题】:Exoplayer v2, live video stream 【发布时间】:2018-06-08 01:04:45 【问题描述】:

我正在尝试用 ExoPlayer 替换视频视图,以便在我的应用中进行直播。

我无法在 ExoPlayer 上找到任何示例来将我的代码替换为 videoView.setVideoURI("") 和实时视频的实现。

任何实现的人都可以帮忙吗?

谢谢

【问题讨论】:

google.github.io/ExoPlayer/guide.html#getting-started 【参考方案1】:

将波纹管编译添加到您的 gradle:

//Video Playback library
compile 'com.google.android.exoplayer:exoplayer:r2.5.+'

将以下行添加到您的 xml 文件中:

 <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/video_player"
        android:layout_
        android:layout_
        app:resize_mode="fill"></com.google.android.exoplayer2.ui.SimpleExoPlayerView>

在您的 Activity 中编写 celow 代码:

    private SimpleExoPlayer player;
    private TrackSelector trackSelector;
    // replace URL with your url/path
    private String urlToPlay;

    //I have used butterknife you can bind view as per your convinience
    @BindView(R.id.video_player)
    SimpleExoPlayerView feedVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);  
        setUpVideo();
    
    private void setUpVideo() 
        // 1. Create a default TrackSelector
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
        trackSelector = new 
        DefaultTrackSelector(videoTrackSelectionFactory);

        // 2. Create the player
        player = ExoPlayerFactory.newSimpleInstance(this, 
                 trackSelector);

        feedVideo.setPlayer(player);
        setUpVideoData();
    

 private void setUpVideoData() 
        // 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, "yourApplicationName"), bandwidthMeter);
        // 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(urlToPlay),
                dataSourceFactory, extractorsFactory, null, null);
        // Prepare the player with the source.
        player.prepare(videoSource);

        // Auto Play video as soon as it buffers
        player.setPlayWhenReady(true);
    

   @Override
    public void onPause() 
        super.onPause();
        pauseLivePreview();
    

    @Override
    public void onResume() 
        super.onResume();
        resumeLivePreview();
    

    private void resumeLivePreview() 
        if (player != null) 
            player.setPlayWhenReady(true);
        
    

    private void pauseLivePreview() 
        if (player != null) 
        if (feedVideo != null && feedVideo.getPlayer() != null) 
            feedVideo.getPlayer().release();
        
    

参考文献 https://github.com/google/ExoPlayer https://google.github.io/ExoPlayer/guide.html

【讨论】:

我没听懂你能不能请详细说明为什么你需要在暂停和恢复时使用搜索栏

以上是关于Exoplayer v2,实时视频流的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序 live-player 实时音视频播放 组件

微信小程序 live-pusher 实时音视频录制 组件

android tv 中的 Exoplayer 不会在直播中重现 (.m3u8) 文件

RecyclerView 和 Firebase Storage 快速滚动 ExoPlayer 异常

灰常越野最新专利--人体动作视频识别考核软件V2.0

手机观看端的实时视频怎么出不来?