Android内嵌VLC实现播放网络视频,网络音频

1、在对应模块的build.gradle文件中,添加依赖

//VlC
    implementation "de.mrmaffen:vlc-android-sdk:2.0.6"

2、布局文件中加入SurfaceView要作为VLC的容器 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".VLCActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SurfaceView
            android:id="@+id/surView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center" />
    </FrameLayout>
</RelativeLayout>

3、VLC播放视频

  

    String url ="网络视频的路径";

        mSurface = findViewById(R.id.surView);
        options.add("--aout=opensles");
        options.add("--audio-time-stretch"); // time stretching
        options.add("-vvv"); // verbosity
        libVLC = new LibVLC(getApplication(), options);
        try {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
            mediaPlayer = new MediaPlayer(libVLC);

            holder = mSurface.getHolder();
            holder.setKeepScreenOn(true);
            mediaPlayer.getVLCVout().setVideoSurface(holder.getSurface(), mSurface.getHolder());
            mediaPlayer.getVLCVout().addCallback(this);
            //播放前还要调用这个方法
            mediaPlayer.getVLCVout().attachViews();

            final Media media = new Media(libVLC, Uri.parse(url));

            mediaPlayer.setEventListener(new MediaPlayer.EventListener() {
                @Override
                public void onEvent(MediaPlayer.Event event) {
                    Log.d("MediaPlayer", "onEvent: " + String.valueOf(event.type));
                    if (event.type == MediaPlayer.Event.EndReached) {
                        Log.d("MediaPlayer", "onEvent:  MediaPlayer.Event.EndReached");
                        mediaPlayer.setMedia(media);
                        mediaPlayer.play();
                    }
                }
            });
            mediaPlayer.setMedia(media);
            mediaPlayer.play();

        } catch (Exception e) {
            e.printStackTrace();
        }
@Override
    protected void onPause() {
        super.onPause();
        if (mediaPlayer != null) {
            mediaPlayer.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mediaPlayer != null) {
            mediaPlayer.play();
        }
    }

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

 // TODO: handle this cleaner
    private void releasePlayer() {
        if (libVLC == null)
            return;
        mediaPlayer.stop();
        final IVLCVout vout = mediaPlayer.getVLCVout();
        vout.removeCallback(this);
        vout.detachViews();
        holder = null;
        libVLC.release();
        libVLC = null;

        mVideoWidth = 0;
        mVideoHeight = 0;
    }

 

4、Activity 需要实现IVLCVout.Callback 接口,当屏幕切换的时候 改变SurfaceView的大小

  改变SurfaceView的大小的方法

/*************
     * Surface
     *************/
    private void setSize(int width, int height) {
        mVideoWidth = width;
        mVideoHeight = height;
        if (mVideoWidth * mVideoHeight <= 1)
            return;

        if (holder == null || mSurface == null)
            return;

        // get screen size
        int w = getWindow().getDecorView().getWidth();
        int h = getWindow().getDecorView().getHeight();

        // getWindow().getDecorView() doesn't always take orientation into
        // account, we have to correct the values
        boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        if (w > h && isPortrait || w < h && !isPortrait) {
            int i = w;
            w = h;
            h = i;
        }

        float videoAR = (float) mVideoWidth / (float) mVideoHeight;
        float screenAR = (float) w / (float) h;

        if (screenAR < videoAR)
            h = (int) (w / videoAR);
        else
            w = (int) (h * videoAR);

        // force surface buffer size
        holder.setFixedSize(mVideoWidth, mVideoHeight);

        // set display size
        ViewGroup.LayoutParams lp = mSurface.getLayoutParams();
        lp.width = w;
        lp.height = h;
        mSurface.setLayoutParams(lp);
        mSurface.invalidate();
    }

  调用地方:

  @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setSize(mVideoWidth, mVideoHeight);
    }
 @Override
    public void onNewLayout(IVLCVout vlcVout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
        if (width * height == 0)
            return;

        // store video size
        mVideoWidth = width;
        mVideoHeight = height;
        setSize(mVideoWidth, mVideoHeight);
    }

    @Override
    public void onSurfacesCreated(IVLCVout vlcVout) {

    }

    @Override
    public void onSurfacesDestroyed(IVLCVout vlcVout) {

    }

    @Override
    public void onHardwareAccelerationError(IVLCVout vlcVout) {

    }

 

posted @ 2018-09-21 14:57 _York 阅读(...) 评论(...) 编辑 收藏