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<String> options) throws IllegalStateException {
if (libVLC == null) {
if (options == null) {
libVLC = new LibVLC();
} else {
libVLC = new LibVLC(options);
}
}
return libVLC;
}
}
添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:padding="20dp"
/>
<TextView
android:id="@+id/mediaStopBt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="停止"
android:textColor="@android:color/black"
android:textColorHint="@color/colorAccent"
android:textSize="30sp" />
</RelativeLayout>
主要逻辑
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的效果
device-2018-05-20-200056.png
网友评论