Android vlc 简单使用

第一步:

  //Vlc 播放器
    implementation group: 'de.mrmaffen', name: 'vlc-android-sdk', version: '3.0.0'

第二步:

 private LibVLC libVLC;
  private  IVLCVout ivlcVout;
    private ArrayList options;
    private org.videolan.libvlc.MediaPlayer mediaPlayer;

 public void setVlc() {
        options=new ArrayList<>();
        options.add(":file-caching=500");//文件缓存
        options.add(":network-caching=500");//网络缓存
        options.add(":live-caching=500");//直播缓存
        options.add(":sout-mux-caching=500");//输出缓存
        options.add(":codec=mediacodec,iomx,all");
        options.add(":rtsp-frame-buffer-size=1000"); //RTSP帧缓冲大小,默认大小为100000
        options.add(":rtsp-tcp");//RTSP采用TCP传输方式
        options.add(":sout-rtp-proto={dccp,sctp,tcp,udp,udplite}");//RTSP采用TCP传输方式

        jzVideo = findViewById(R.id.jz_video);
        libVLC = LibVlcSingle.getLibVLC(options);
        mediaPlayer = new org.videolan.libvlc.MediaPlayer(libVLC);
        ivlcVout = mediaPlayer.getVLCVout();
        ivlcVout.setVideoView(jzVideo);
        ivlcVout.attachViews();
    }

public void play(String path) {
        media=new Media(libVLC,Uri.parse(path));
        media.setHWDecoderEnabled(false, false);

        mediaPlayer.setMedia(media);
        media.release();

        mediaPlayer.play();
        media.setEventListener(new Media.EventListener() {
            @Override
            public void onEvent(Media.Event event) {
                Log.d("VLC- 播放状态:", event.type + "");
                switch (event.type) {
                    case Opening://打开中
                        showDialog1("打开中");
                        break;
                    case Buffering://缓冲中
                        showDialog1("缓冲中");
                        break;
                    case Paused://暂停中
                        showDialog1("暂停中");
                        break;
                    case Error://播放出错
                        showDialog1("播放出错");
                        break;
                    case Ended://播放出错
                        Toast.makeText(LiveActivity.this, "播放完成", Toast.LENGTH_SHORT).show();
                        break;
                    case Playing://播放中
                        if (huds != null) {
                            if (huds.isShowing()) {
                                huds.dismiss();

                            }
                        }
                        break;
                    default:
                        if (huds != null) {
                            if (huds.isShowing()) {
                                huds.dismiss();

                            }
                        }
                        break;
                }
            }
        });

    }

xml

 


单例:
public class LibVlcSingle {
    private static LibVLC libVLC = null;
    public synchronized static LibVLC getLibVLC(ArrayList options) {
        if (libVLC == null) {
            if (options == null) {
                libVLC = new LibVLC();
            } else {
                libVLC = new LibVLC(options);
            }
        }
        return libVLC;
    }
}

你可能感兴趣的:(Android)