2018-05-20 安卓使用vlc播放本地视频

2018-05-16添加libvlc.aar

https://www.jianshu.com/p/f961ffc7dfc6

单例的libvlc

package com.example.lybly.myapplication.util;

import org.videolan.libvlc.LibVLC;

import java.util.ArrayList;

/**
 * LibVLCUtil LibVLC 单例
 */
public class LibVLCUtil {
    private static LibVLC libVLC = null;

    public synchronized static LibVLC getLibVLC(ArrayList options) throws IllegalStateException {
        if (libVLC == null) {
            if (options == null) {
                libVLC = new LibVLC();
            } else {
                libVLC = new LibVLC(options);
            }
        }
        return libVLC;
    }
}

添加权限

 

布局文件




    

    


主要逻辑

package com.example.lybly.myapplication;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import com.example.lybly.myapplication.util.LibVLCUtil;

import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;


public class MainActivity extends AppCompatActivity {

    private IVLCVout vlcVout;
    private MediaPlayer mediaPlayer;

    private SurfaceView surfaceView;
    private TextView mediaStopBt;
    private boolean isMediaStop = false;
    private long totalTime = 0;
    private IVLCVout.Callback callback;
    private MediaPlayer.EventListener eventListener = new MediaPlayer.EventListener() {
        @Override
        public void onEvent(MediaPlayer.Event event) {
            try {
                if (event.getTimeChanged() == 0 || totalTime == 0 || event.getTimeChanged() > totalTime) {
                    return;
                }
                //可以用来刷新播放进度条
                //播放结束
                if (mediaPlayer.getPlayerState() == Media.State.Ended) {
                    mediaPlayer.setTime(0);
                    mediaPlayer.stop();
                }
            } catch (Exception e) {
                Log.d("vlc-event", e.toString());
            }
        }
    };
    ;

    private int videoWidth;
    private int videoHight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView = findViewById(R.id.surfaceView);

        LibVLC libvlc = LibVLCUtil.getLibVLC(null);

        mediaPlayer = new MediaPlayer(libvlc);
        vlcVout = mediaPlayer.getVLCVout();

        //视频信息,设置视频播放器窗口
        callback = new IVLCVout.Callback() {
            @Override
            public void onNewLayout(IVLCVout ivlcVout, int i, int i1, int i2, int i3, int i4, int i5) {
                try {
                    //获取播放时长,长宽
                    totalTime = mediaPlayer.getLength();
                    videoWidth = i;
                    videoHight = i1;


                } catch (Exception e) {
                    Log.d("vlc-newlayout", e.toString());
                }
            }

            @Override
            public void onSurfacesCreated(IVLCVout ivlcVout) {

            }

            @Override
            public void onSurfacesDestroyed(IVLCVout ivlcVout) {

            }
        };
        vlcVout.addCallback(callback);
        vlcVout.setVideoView(surfaceView);
        vlcVout.attachViews();

        Media media;
        //远程播放还是本地播放
        media = new Media(libvlc, "/sdcard/a.mp4");
        mediaPlayer.setMedia(media);


        //播放中的监听器,播放时候不同时间的监听器
        mediaPlayer.setEventListener(eventListener);

        mediaStopBt = (TextView) findViewById(R.id.mediaStopBt);
        mediaStopBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isMediaStop = !isMediaStop;
                if (isMediaStop) {
                    mediaStopBt.setText("继续");
                    mediaPlayer.pause();
                } else {
                    mediaStopBt.setText("停止");
                    mediaPlayer.play();
                }
            }
        });

        mediaPlayer.play();

    }

    private void pausePlay() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }

        vlcVout.detachViews();
        vlcVout.removeCallback(callback);
        mediaPlayer.setEventListener(null);
    }

    private void resumePlay() {
        vlcVout.setVideoView(surfaceView);
        vlcVout.attachViews();

        vlcVout.addCallback(callback);
        mediaPlayer.setEventListener(eventListener);
    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();

            pausePlay();
            mediaPlayer.release();
        } catch (Exception e) {
            Log.d("vlc-destroy", e.toString());
        }
    }

    @Override
    protected void onStop() {
        try {
            super.onStop();

            pausePlay();
        } catch (Exception e) {
            Log.d("vlc-stop", e.toString());
        }
    }

    @Override
    protected void onPause() {
        try {
            super.onPause();
            mediaStopBt.setText("继续");
            isMediaStop = !isMediaStop;
            pausePlay();
        } catch (Exception e) {
            Log.d("vlc-pause", e.toString());
        }
    }

    @Override
    protected void onResume() {
        try {
            super.onResume();
            resumePlay();
        } catch (Exception e) {
            Log.d("vlc-resume", e.toString());
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        try {
            super.onNewIntent(intent);
        } catch (Exception e) {
            Log.d("vlc-newintent", e.toString());
        }
    }

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();

            finish();
        } catch (Exception e) {
            Log.d("vlc-back", e.toString());
        }
    }
}

vlc在c8815上播放1080p的效果


2018-05-20 安卓使用vlc播放本地视频_第1张图片
device-2018-05-20-200056.png

你可能感兴趣的:(2018-05-20 安卓使用vlc播放本地视频)