我的 Android 设备可以直接播放相同的视频 MP4 和 AVI 视频文件,但在使用源代码时,在显示“正在加载视频”消息的进度对话框后,出现错误“无法播放此视频”。 。 我正在使用两种方式: 1.第一种方法:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("http://localhost/h.mp4");
intent.setDataAndType(data, "video/*");
startActivity(intent);
第二种方法:
Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
有什么帮助吗?
最佳答案
您可以尝试VideoView
。 (Documentation)
try{
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set Audio/Video
String strfilename = "http://localhost/h.mp4";
Uri video = Uri.parse(strfilename);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
catch (Exception e) {
//Handle Errors
}
编辑
Android 在使用 videoView 时对视频编码方式也很挑剔。 (参见下面引用 link )
For video content that is streamed over HTTP or RTSP, there are additional requirements:
•For 3GPP and MPEG-4 containers, the moov atom must precede any mdat atoms, but must succeed the ftyp atom.
•For 3GPP, MPEG-4, and WebM containers, audio and video samples corresponding to the same time offset may be no more than 500 KB apart. To minimize this audio/video drift, consider interleaving audio and video in smaller chunk sizes.
关于java - 无法使用 Android 默认视频播放器使用 Intent 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450373/