这可以通过稍微改变 libcocos2dx 中的 Cocos2dxActivity 类,在其中添加 VideoView 来实现。
首先,将此 xml 布局添加到 libcocos2dx 中的 res/layout 中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<VideoView
android:id="@+id/video"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="4" />
<FrameLayout
android:id="@+id/frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6" >
</FrameLayout>
</LinearLayout>
此布局将成为您应用的新布局。
其次,您必须将init()
Cocos2dxActivity 中的方法更改为:
public void init() {
// Set our new layout as the content view
setContentView(R.layout.test);
// FrameLayout
//ViewGroup.LayoutParams framelayout_params =
//new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
//ViewGroup.LayoutParams.FILL_PARENT);
//FrameLayout framelayout = new FrameLayout(this);
//framelayout.setLayoutParams(framelayout_params);
Resources res = this.getResources();
int id = res.getIdentifier("YOUR_VIDEO_NAME_HERE", "raw", getPackageName());
if (id == 0) {
Log.e("Cocos2dx VIDEO", "ERROR! Resource id == 0");
Log.e("Cocos2dx VIDEO", "ERROR! Check name of video!");
} else {
VideoView vv = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + id));
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp != null)
mp.setLooping(true);
});
vv.requestFocus();
vv.start();
}
FrameLayout framelayout = (FrameLayout) findViewById(R.id.frame);
// Cocos2dxEditText layout
ViewGroup.LayoutParams edittext_layout_params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Cocos2dxEditText edittext = new Cocos2dxEditText(this);
edittext.setLayoutParams(edittext_layout_params);
// ...add to FrameLayout
framelayout.addView(edittext);
// Cocos2dxGLSurfaceView
this.mGLSurfaceView = this.onCreateView();
// ...add to FrameLayout
framelayout.addView(this.mGLSurfaceView);
// Switch to supported OpenGL (ARGB888) mode on emulator
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(framelayout);
}
这将为您提供一个覆盖屏幕左侧 40% 的 VideView 和覆盖右侧 60% 的 cocos 游戏区域。
您应该将视频放在项目的 res/raw 文件夹中。
此外,这是仅限 Android 的解决方案(基于您在问题中提供的标签)。要在 iOS 或其他平台上实现这一点,您必须对这些平台的 cocos 库代码进行其他更改。
编辑:记得清理(只是为了安全)并重建库。