如何播放本地视频文件
How to play a local Video file
我正在尝试播放视频,但什么也没发生:
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
您可以直接在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
To get videoUri from video path then do like below
Uri videoUri = Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4"));
我相信您看不到屏幕上播放的任何视频,因为您以编程方式创建的图形小部件实际上与 activity 的布局无关。
最快的解决方案可能如下:
- 通过xml
定义一个LinearLayout
获取它的引用并使用它来附加视频视图
线性布局 linearLayout = findViewBy(R.id.yourlinearlayoutid)
LayoutParams videoView_LP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
VideoView videoView = 新 VideoView(RevLibGenConstantine.REV_CONTEXT);
videoView.setLayoutParams(videoView_LP);
videoView.setVideoPath(视频路径);
videoView.requestFocus();
videoView.start();
linearLayout.addView(videoView);
我正在尝试播放视频,但什么也没发生:
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
您可以直接在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
To get videoUri from video path then do like below
Uri videoUri = Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4"));
我相信您看不到屏幕上播放的任何视频,因为您以编程方式创建的图形小部件实际上与 activity 的布局无关。
最快的解决方案可能如下:
- 通过xml 定义一个LinearLayout
获取它的引用并使用它来附加视频视图
线性布局 linearLayout = findViewBy(R.id.yourlinearlayoutid)
LayoutParams videoView_LP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
VideoView videoView = 新 VideoView(RevLibGenConstantine.REV_CONTEXT); videoView.setLayoutParams(videoView_LP);
videoView.setVideoPath(视频路径); videoView.requestFocus(); videoView.start();
linearLayout.addView(videoView);