美文网首页
用SurfaceView 结合VLC的解码库播放远程RTSP流

用SurfaceView 结合VLC的解码库播放远程RTSP流

作者: 陈陈_04d0 | 来源:发表于2020-04-25 15:31 被阅读0次

    package cc.lcsunm.android.donghuan.module.mrl;

    import android.app.Activity;

    import android.net.Uri;

    import android.view.SurfaceView;

    import androidx.lifecycle.MutableLiveData;

    import org.videolan.libvlc.LibVLCFactory;

    import org.videolan.libvlc.Media;

    import org.videolan.libvlc.MediaPlayer;

    import org.videolan.libvlc.interfaces.ILibVLC;

    import org.videolan.libvlc.interfaces.ILibVLCFactory;

    import org.videolan.libvlc.interfaces.IVLCVout;

    import org.videolan.libvlc.util.DisplayManager;

    import java.util.ArrayList;

    /**

    * Created by cc on 2020/4/25.

    */

    public class MrlPlayer {

    private static final boolean USE_TEXTURE_VIEW =true;

        private static final boolean ENABLE_SUBTITLES =false;

        private DisplayManagermDisplayManager;

        public MediaPlayermMediaPlayer;

        private MediamMedia =null;

        private SurfaceViewvLayout;

        private ILibVLCmVlc;

        private Stringurl;

        public MrlPlayer(Activity context, SurfaceView layout, int width, int height) {

    vLayout = layout;

            mDisplayManager =new DisplayManager(context, new MutableLiveData<>(), false, false, false);

            ILibVLCFactory libVLCFactory =new LibVLCFactory();//(ILibVLCFactory) FactoryManager.getFactory(ILibVLCFactory.factoryId);

            ArrayList options =new ArrayList();

            //options.add("--subsdec-encoding ");

            options.add("-vvv");

            options.add("--rtsp-tcp");

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

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

    //        options.add("--custom-aspect-ratios=" + width + ":" + (width * 9 / 16));

    //        options.add("--avcodec-codec=h264");

    //        options.add("--avcodec-fast");

    //        options.add("--avcodec-skiploopfilter=4");

    //        options.add("--avcodec-dr");

            mVlc = libVLCFactory.getFromOptions(context, options);//libVLCFactory.getFromContext(this);

            mMediaPlayer =new MediaPlayer(mVlc);

            IVLCVout vlcVout =mMediaPlayer.getVLCVout();

            vlcVout.setVideoView(vLayout);

            vlcVout.attachViews();

            vlcVout.setWindowSize(width, height);//宽,高  播放窗口的大小

            mMediaPlayer.setAspectRatio("3:2");//宽,高  画面大小

            mMediaPlayer.setScale(0);

            //  因为没法动态设置视频高宽不能铺满所以用surfaceView    是否使用字幕              //是否使用TEXTURE_VIEW

    // mMediaPlayer.attachViews(vLayout, mDisplayManager, ENABLE_SUBTITLES, USE_TEXTURE_VIEW);

        }

    private MediamakeMedia(String url) {

    Media m =new Media(mVlc, Uri.parse(url));

            int cache =1000;

            m.addOption(":no-audio");

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

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

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

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

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

            //m.addOption(":rfb-quality-level=2");

            return m;

        }

    public void play(String url) {

    if (mMediaPlayer.isPlaying()) {

    mMediaPlayer.stop();

            }

    this.url = url;

            mMedia = makeMedia(url);

            mMediaPlayer.play(mMedia);

        }

    public void onStop() {

    if (mMediaPlayer.isPlaying()) {

    mMediaPlayer.stop();

            }

    if (mMediaPlayer.getVLCVout().areViewsAttached()) {

    mMediaPlayer.detachViews();

            }

    }

    public void onStart() {

    if (!mMediaPlayer.getVLCVout().areViewsAttached()) {

    IVLCVout ivlcVout =mMediaPlayer.getVLCVout();

                ivlcVout.setVideoView(vLayout);

                ivlcVout.attachViews();

                // mMediaPlayer.attachViews(vLayout, mDisplayManager, ENABLE_SUBTITLES, USE_TEXTURE_VIEW);

            }

    if (!mMediaPlayer.isPlaying() &&mMedia !=null) {

    /* if (mMedia.isReleased())

    makeMedia(url);*/

                mMediaPlayer.play();

            }

    }

    public void onDestroy() {

    if (!mVlc.isReleased()) {

    mVlc.release();

            }

    if (mMediaPlayer !=null && !mVlc.isReleased())

    mMediaPlayer.release();

        }

    }

    相关文章

      网友评论

          本文标题:用SurfaceView 结合VLC的解码库播放远程RTSP流

          本文链接:https://www.haomeiwen.com/subject/hhrkwhtx.html