我正在尝试播放视频,但什么都没有发生:
Log.v("MyApp", "PATH : " + videoPath);
LinearLayout linearLayout = new LinearLayout(mContext);
LayoutParams videoView_LP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
VideoView videoView = new VideoView(RevLibGenConstantine.REV_CONTEXT);
videoView.setLayoutParams(videoView_LP);
videoView.setVideoPath(videoPath);
videoView.requestFocus();
videoView.start();
linearLayout.addView(videoView);
我在做什么不对?
视频文件路径为/storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4
答案 0 :(得分:1)
您可以直接在videoview
中设置布局参数并使用setVideoURI
,请参考以下代码并进行检查。
RelativeLayout relativeLayout = findViewById(R.id.yourrelativelayout);
LinearLayout linearLayout = new LinearLayout(mContext);
VideoView video = new VideoView(this);
video.setVideoURI(videoUri);
video.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
video.requestFocus();
video.start();
linearLayout.addView(video);
// please attach above layout to your xml view
relativeLayout.addView(linearLayout); // here you can either relative or linear
要从视频路径获取videoUri,请按以下步骤操作
Uri videoUri = Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4"));
答案 1 :(得分:1)
我相信您不会在屏幕上看到任何正在播放的视频,因为您以编程方式创建的图形小部件实际上并未与活动的布局相关联。
最快的解决方案可能是:
获取参考并使用它来附加视频视图
LinearLayout linearLayout = findViewBy(R.id.yourlinearlayoutid)
LayoutParams videoView_LP =新的LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
VideoView videoView =新的VideoView(RevLibGenConstantine.REV_CONTEXT); videoView.setLayoutParams(videoView_LP);
videoView.setVideoPath(videoPath); videoView.requestFocus(); videoView.start();
linearLayout.addView(videoView);