• 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) {
    
        }
  • 相关阅读:
    POJ 1837 Balance 水题, DP 难度:0
    POJ 3126 Prime Path bfs, 水题 难度:0
    python学习笔记day01 练习题
    python学习笔记day01_08 循环语句while
    python学习笔记day01_07 流程控制语句
    python学习笔记day01_06 基础数据类型
    python学习笔记day01_05 运行py程序--常量,变量,注释
    python学习笔记day01_04 python分类
    python学习笔记-day01_02计算机基础
    PAT 甲级 1134 Vertex Cover
  • 原文地址:https://www.cnblogs.com/tangchun/p/9686570.html
Copyright © 2020-2023  润新知