【Android直播流加载】

这里总结一下视频直播流播放的相关知识。顺便也整理一下如何获取一个视频的地址,网上找了一下很多央视和卫视的地址都已经失效了。

VLC-Android

很遗憾github上官方 VLC-Android 上看了半天没看明白怎么用。。。这篇文章比较一目了然:Android使用VLC播放视频和直播流

这里我也整理一下用法:

implementation 'org.videolan.android:libvlc-all:3.3.5'

下载后将 libvlc 文件夹放入app的同级目录下:
【Android直播流加载】_第1张图片

VideoPlayActivity 类,使用了一个工具类: implementation ‘com.blankj:utilcodex:1.31.0’

public class VideoPlayActivity extends AppCompatActivity {

    private LibVLC mLibVLC = null;
    private MediaPlayer mMediaPlayer = null;
    private SurfaceView textureView;
    String str_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textureView = findViewById(R.id.texture_video);
        str_url = getIntent().getStringExtra("url").toString();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaPlayer.release();
        mLibVLC.release();
    }

    @Override
    protected void onStart() {
        super.onStart();

        final ArrayList args = new ArrayList<>();//VLC参数

        args.add("--rtsp-tcp");//强制rtsp-tcp,加快加载视频速度

        args.add("--aout=opensles");

        args.add("--audio-time-stretch");

        //args.add("--sub-source=marq{marquee=\"%Y-%m-%d,%H:%M:%S\",position=10,color=0xFF0000,size=40}");//这行是可以再vlc窗口右下角添加当前时间的

        args.add("-vvv");

        mLibVLC = new LibVLC(this, args);

        mMediaPlayer = new MediaPlayer(mLibVLC);

        Rect surfaceFrame = textureView.getHolder().getSurfaceFrame();

        //设置vlc视频铺满布局
        mMediaPlayer.getVLCVout().setWindowSize(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());//宽,高  播放窗口的大小
        // mMediaPlayer.setAspectRatio(ScreenUtils.getScreenWidth() + ":" + ScreenUtils.getScreenHeight());//宽,高  画面大小
        mMediaPlayer.setScale(0);//这行必须加,为了让视图填满布局

        //添加视图

        IVLCVout vout = mMediaPlayer.getVLCVout();

        vout.setVideoView(textureView);

        vout.attachViews();

        // String str_url = "https://qywapp.oss-cn-beijing.aliyuncs.com/2022/7/18/oss06ac675d-c493-2121-cfba-e5d627210036.mp4";
        //String str_url="rtsp://admin:[email protected]:554/Streaming/Channels/102";
        //String str_url = "https://media.w3.org/2010/05/sintel/trailer.mp4";

        //美国
        // String str_url = "rtmp://media3.scctv.net/live/scctv_800";
        //韩国
        // String str_url = "rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp";
        // String str_url = "https://v.kuaishou.com/wwZ3v9";
        Uri uri = Uri.parse(str_url);//rtsp流地址或其他流地址//"https://media.w3.org/2010/05/sintel/trailer.mp4"

        final Media media = new Media(mLibVLC, uri);

        int cache = 10;

        media.addOption(":network-caching=" + cache);

        media.addOption(":file-caching=" + cache);

        media.addOption(":live-cacheing=" + cache);

        media.addOption(":sout-mux-caching=" + cache);

        media.addOption(":codec=mediacodec,iomx,all");

        mMediaPlayer.setMedia(media);//

        media.setHWDecoderEnabled(false, false);//设置后才可以录制和截屏,这行必须放在mMediaPlayer.setMedia(media)后面,因为setMedia会设置setHWDecoderEnabled为true

        mMediaPlayer.setEventListener(new MediaPlayer.EventListener() {
            @Override
            public void onEvent(MediaPlayer.Event event) {
                // System.out.println("MainActivity.onEvent 111");
                if (event.type == MediaPlayer.Event.Playing) {
                    System.out.println("MainActivity.onEvent 正在播放");
                } else if (event.type == MediaPlayer.Event.Paused) {
                    System.out.println("MainActivity.onEvent Paused");
                } else if (event.type == MediaPlayer.Event.Opening) {
                    System.out.println("MainActivity.onEvent Opening");
                } else if (event.type == MediaPlayer.Event.Stopped) {
                    System.out.println("MainActivity.onEvent Stopped");
                } else if (event.type == MediaPlayer.Event.Vout) {
                    System.out.println("MainActivity.onEvent getVoutCount=" + event.getVoutCount());
                    //mHandler.sendEmptyMessageDelayed(UPDATE_SCREEN, 1000);
                } else {
                    System.out.println("MainActivity.onEvent else ");
                }

            }
        });
        mMediaPlayer.play();

    }

    @Override
    protected void onStop() {
        super.onStop();
        mMediaPlayer.stop();
        mMediaPlayer.detachViews();
    }
}

布局文件:




    


参考文章:

Android使用VLC播放摄像头RTSP流

你可能感兴趣的:(【andorid视频】,android,音视频)