cocos2d-x深坑之VideoPlayer类 C++
环境:
1、cocos2d-x 3.17.2
2、andtoid studio 3.5.3 【2019年12月最新版】
3、java 1.8
4、visual studio 2015
5、Sdk Tools 26.1.1
6、Ndk android-ndk-r19c
C++代码
引用
场景Scene init()代码
以上代码就可以在移动设备上播放视频了,iOS没有测试
做背景要解决以下问题,不然视频始终在最顶层,遮挡了其它层。修改如下:
在Android studio中找到
修改如下:双击打开,右侧编辑代码
private void _createVideoView(int index) {
Cocos2dxVideoView videoView = new Cocos2dxVideoView(mActivity,index);
sVideoViews.put(index, videoView);
FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(videoView, lParams);
videoView.setZOrderOnTop(false); //修改这里为false 不置顶
videoView.setZOrderMediaOverlay(false); //这里android 8.0及以上需要添加,否则还是会置顶
videoView.setOnCompletionListener(videoEventListener);
}
Android8.0以上还需要做以下修改:双击打开,右侧编辑代码
添加两行代码即可,看最下方:
public void init() {
// FrameLayout
ViewGroup.LayoutParams framelayout_params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mFrameLayout = new ResizeLayout(this);
mFrameLayout.setLayoutParams(framelayout_params);
// Cocos2dxEditText layout
ViewGroup.LayoutParams edittext_layout_params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Cocos2dxEditBox edittext = new Cocos2dxEditBox(this);
edittext.setLayoutParams(edittext_layout_params);
mFrameLayout.addView(edittext);
// Cocos2dxGLSurfaceView
this.mGLSurfaceView = this.onCreateView();
this.mGLSurfaceView.setPreserveEGLContextOnPause(true);
// ...add to FrameLayout
mFrameLayout.addView(this.mGLSurfaceView);
// Switch to supported OpenGL (ARGB888) mode on emulator
// this line dows not needed on new emulators and also it breaks stencil buffer
//if (isAndroidEmulator())
// this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
this.mGLSurfaceView.setCocos2dxEditText(edittext);
// Set framelayout as the content view
setContentView(mFrameLayout);
//在这里添加这两行代码即可
this.mGLSurfaceView.setZOrderOnTop(true);
this.mGLSurfaceView.setZOrderMediaOverlay(true);
}
试试效果吧!
以上测试结果出现问题,用VideoPlayer做背景后发现MenuItem类的按钮会出现触摸无效,最后找出问题所在,原因是VideoPlayer的层吞噬了触摸事件,只需要在下面修改一下即可,看图: