我正在尝试在cocos2d-x表面视图之上进行VideoView
工作。我现在可以使用以下代码片段加载和播放视频:
初始化课程时:
// Create the LinearLayout that will contain our VideoView
_videoLayout = new LinearLayout( _activity );
_videoLayout.setId( VIDEO_VIEW_ID );
_videoLayout.setGravity( Gravity.CENTER );
_videoLayout.setBackgroundColor( Color.BLACK );
_videoLayout.setOrientation( LinearLayout.VERTICAL );
_videoLayout.bringToFront();
// Add the LinearLayout to the current Activity
_activity = MyGame.getCocosActivity();
_activity.addContentView( _videoLayout, new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
播放视频时:
// When the video is ready to be played, create a VideoView
VideoView videoView = new VideoView( Cocos2dxActivity.getContext() );
videoView.setLayoutParams( new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
// Add it to the _videoLayout object
LinearLayout layout = ( LinearLayout ) activity.findViewById( VIDEO_VIEW_ID );
layout.setVisibility( View.VISIBLE );
layout.addView( videoView );
// Play the video
videoView.setVideoURI( Uri.parse( path ) );
videoView.requestFocus();
videoView.start();
以上代码产生以下结果:
我的预期结果如何:
我似乎无法弄清楚如何完成这两项工作。有帮助吗?我对Android框架没有太多经验,所以请耐心等待。
注意:我不想使用XML来创建布局。
答案 0 :(得分:0)
为什么不想使用XML布局?它们就是为此设计的,并且在您掌握它们之后,让您的生活更轻松。
无论如何:在我的情况下,我在RelativeLayout上添加了我的VideoView并将其CenterHorizontal和CenterVertical参数设置为true。宽度和高度设置为wrap_content
。这是用XML完成的,所以我不能为你提供Java等价物。
还可以尝试在布局上调用bringToFront()
,以确保视频位于CocosView之上。