/**
* created by XuTi on 2019/5/20 14:00
*/
public class VlcModel {
private static final String TAG = VlcModel.class.getSimpleName();
private LibVLC mLibVLC;
private MediaPlayer mMediaPlayer;
private String mUriString;
private SurfaceView mSurfaceView = null;
private MediaPlayer.EventListener mEventListener;
private int mPortraitDisplayWidth;
private int mPortraitDisplayHeight;
private int mLandscapeDisplayWidth;
private int mLandscapeDisplayHeight;
public VlcModel(String uriString,MediaPlayer.EventListener eventListener) {
mUriString = uriString;
mEventListener = eventListener;
}
private void init() {
if (mMediaPlayer == null || mMediaPlayer.isReleased()) {
final ArrayList<String> args = new ArrayList<>();
args.add("-vvv");
mLibVLC = new LibVLC(mSurfaceView.getContext(), args);
mMediaPlayer = new MediaPlayer(mLibVLC);
}
}
public void onCreate(SurfaceView surfaceView) {
mSurfaceView = surfaceView;
init();
{
Context context = surfaceView.getContext();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
int portraitDisplayWidth;
int portraitDisplayHeight;
int landscapeDisplayWidth;
int landscapeDisplayHeight;
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
portraitDisplayWidth = screenWidth;
landscapeDisplayWidth = screenHeight;
landscapeDisplayHeight = screenWidth;
} else {
portraitDisplayWidth = screenHeight;
landscapeDisplayWidth = screenWidth;
landscapeDisplayHeight = screenHeight;
}
portraitDisplayHeight = portraitDisplayWidth * 9 / 16;
mPortraitDisplayWidth = portraitDisplayWidth;
mPortraitDisplayHeight = portraitDisplayHeight;
mLandscapeDisplayWidth = landscapeDisplayWidth;
mLandscapeDisplayHeight = landscapeDisplayHeight;
}
}
public void attachViews() {
init();
mMediaPlayer.setEventListener(mEventListener);
final IVLCVout vlcVout = mMediaPlayer.getVLCVout();
if (!vlcVout.areViewsAttached()) {
vlcVout.addCallback(mCallback);
vlcVout.setVideoView(mSurfaceView);
vlcVout.attachViews(mOnNewVideoLayoutListener);
play();
}
}
public void detachViews() {
if (mMediaPlayer != null && mMediaPlayer.getVLCVout() != null &&mMediaPlayer.getVLCVout().areViewsAttached()) {
mMediaPlayer.stop();
mMediaPlayer.getVLCVout().detachViews();
}
}
public void onDestroy() {
if (mMediaPlayer != null&&!mMediaPlayer.isReleased()) {
mMediaPlayer.release();
}
if (mLibVLC != null&&!mLibVLC.isReleased()) {
mLibVLC.release();
}
}
private IVLCVout.Callback mCallback = new IVLCVout.Callback() {
@Override
public void onSurfacesCreated(IVLCVout vlcVout) {
}
@Override
public void onSurfacesDestroyed(IVLCVout vlcVout) {
}
};
private IVLCVout.OnNewVideoLayoutListener mOnNewVideoLayoutListener = new IVLCVout.OnNewVideoLayoutListener() {
@Override
public void onNewVideoLayout(IVLCVout vlcVout, int width, int height, int visibleWidth, int visibleHeight, int sarNum, int sarDen) {
// updateVideoSurfaces();
}
};
public void play() {
if (mMediaPlayer.getVLCVout().areViewsAttached()) {
final Media media = new Media(mLibVLC, Uri.parse(mUriString));
media.setHWDecoderEnabled(true, true);
int cache = 220;
media.addOption(":network-caching=" + cache);
media.addOption(":file-caching=" + cache);
media.addOption(":live-caching=" + cache);
media.addOption(":sout-mux-caching=" + cache);
media.addOption(":codec=mediacodec,iomx,all");
// media.addOption(":clock-jitter=400");
// media.addOption(":clock-synchro=500");
//decoder all media type
// media.addOption(":codec=ALL");
mMediaPlayer.setMedia(media);
mMediaPlayer.play();
}
}
/**
* 横竖屏切换的时候调用
*/
public void updateVideoSurfaces() {
final boolean isPortrait = mSurfaceView.getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
double displayWidth;
double displayHeight;
String aspectRatio;
if (isPortrait) {
displayWidth = mPortraitDisplayWidth;
displayHeight = mPortraitDisplayHeight;
} else {
displayWidth = mLandscapeDisplayWidth;
displayHeight = mLandscapeDisplayHeight;
}
aspectRatio = displayWidth + ":" + displayHeight;
mMediaPlayer.getVLCVout().setWindowSize((int) displayWidth, (int) displayHeight);
mMediaPlayer.setAspectRatio(aspectRatio);
mMediaPlayer.setScale(0);
ViewGroup.LayoutParams layoutParams = mSurfaceView.getLayoutParams();
layoutParams.width = (int) displayWidth;
layoutParams.height = (int) displayHeight;
mSurfaceView.setLayoutParams(layoutParams);
}
public int getLandscapeDisplayHeight() {
return mLandscapeDisplayHeight;
}
/**
* 横屏全屏时,Build.VERSION.SDK_INT >= Build.VERSION_CODES.M 才能使状态栏漂浮在Activity之上,否则就得减去状态栏的高度
* @param landscapeDisplayHeight
*/
public void setLandscapeDisplayHeight(int landscapeDisplayHeight) {
mLandscapeDisplayHeight = landscapeDisplayHeight;
}
}
/**
* created by XuTi on 2019/5/23 9:29
*/
public class PlayActivity extends AppCompatActivity {
private VlcModel mVlcModel;
private Handler mHandler = MyThreadUtils.getThreadHandler();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
mVlcModel = new VlcModel("rtsp://192.168.1.254/xxxx.mov",mEventListener);
mVlcModel.onCreate((SurfaceView) findViewById(R.id.surfaceV));
mVlcModel.updateVideoSurfaces();
}
@Override
protected void onResume() {
super.onResume();
mHandler.post(new Runnable() {
@Override
public void run() {
mVlcModel.attachViews();
}
});
}
@Override
protected void onPause() {
super.onPause();
mHandler.post(new Runnable() {
@Override
public void run() {
mVlcModel.detachViews();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.post(new Runnable() {
@Override
public void run() {
mVlcModel.onDestroy();
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mVlcModel.updateVideoSurfaces();
}
MediaPlayer.EventListener mEventListener = new MediaPlayer.EventListener() {
@Override
public void onEvent(MediaPlayer.Event event) {
switch (event.type) {
case MediaPlayer.Event.EndReached:
LogUtils.d("The connection is interrupted, reconnected");
mVlcModel.play();
break;
case MediaPlayer.Event.Playing:
LogUtils.d("The connection is successful and starts playing");
ToastUtils.showShort("playing");
break;
case MediaPlayer.Event.Paused:
break;
case MediaPlayer.Event.Stopped:
break;
case MediaPlayer.Event.Opening:
LogUtils.d("Connecting, please wait ...");
ToastUtils.showShort("waitting");
break;
case MediaPlayer.Event.PositionChanged:
break;
case MediaPlayer.Event.EncounteredError:
LogUtils.d("Connection failed, reconnected");
mVlcModel.play();
break;
default:
break;
}
}
};
}
多语言切换时,activity中onConfigurationChanged处理多语言,另外一篇讲多语言的已解决
来源:CSDN
作者:android xt
链接:https://blog.csdn.net/qq_17769347/article/details/104003757