如何在android中播放视频文件?
我将视频 MP4 放置到我的域空间中。我有它的公共 URL,现在我想在我的 Android 应用程序中播放它;但不知道我该怎么做。我使用了以下代码,但它不起作用。轨道控制器正在移动,但我在屏幕上看不到任何视频。
public class MPlayer extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playvideo);
VideoView videoView = new VideoView(MPlayer.this);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
videoView.requestFocus();
videoView.start();
LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
l.addView(videoView);
}
}
I am placed video MP4 to my domain space. I have its public URL, Now i want to play it in my android app; but don't know how can I do this. I used following code which is not working. Track controller is moving but I can't see any video on screen.
public class MPlayer extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playvideo);
VideoView videoView = new VideoView(MPlayer.this);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
videoView.requestFocus();
videoView.start();
LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
l.addView(videoView);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
VideoView 类可以从各种来源(例如资源或内容提供程序)加载图像,负责计算视频的测量值,以便可以在任何布局管理器中使用,并提供各种显示选项,例如缩放和着色。
代码:
如果您想查看源代码:使用 VideoView 播放视频文件在 Android 中
The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.
Code:
if you want see source code : Play video file using VideoView in Android
大多数时候,我使用以下代码:
有关更多信息,请查看此页面: http://developer.android.com/guide/topics/media/index.html
和
http://developer.android .com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
Most of the time, I'm using following code:
for more information look at this page: http://developer.android.com/guide/topics/media/index.html
and
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
我认为这可能会帮助您找到一些解决方案。
I think this may help you find some solution.
如果您在模拟器中尝试此操作,则可能必须在真实设备中尝试它,因为有时我也遇到同样的问题。我将无法在模拟器中观看视频,但视频在手机中播放没有任何问题。问题是,我认为是模拟器,而不是你的代码。
If you are trying this in your emulator, you might have to try it in a real device, because sometimes I too use face the same problem. I will not be able to view the video in emulator, but the video will play without any problem in the mobile. the problem is, I think with the emulator, not with your code.
这是我在项目中从网络播放视频文件的方式
需要 Kotlin、AndroidX
在文件缓冲时显示加载对话框,然后开始播放:
Loader XML
**对于网络访问,请在清单,来自 Android P 其必需 **
在 res/xml 中添加 network_security_config.xml
This is how I played a video file from Network in my project
Required Kotlin, AndroidX
Show a loading dialog while the file is buffering and then start playback:
Loader XML
**For Network Access add network config in the manifest, from ANdroid P its required **
Add network_security_config.xml in res/xml
您应该在
onResume
中执行此操作,因为在onCreate
中,VideoView
不知道其大小,无法创建正确的表面来显示视频。You should do it in
onResume
, because inonCreate
VideoView
does not knows its size and can't create properly surface to display video.