【发布时间】:2014-07-03 13:51:03
【问题描述】:
我必须开发一个从 URL 下载视频文件的应用程序 下载后,我正在通过 Intent 播放。
但每次我收到相同的消息时:“您无法播放此视频。”
下载代码:
protected String doInBackground(String... params) {
File file = new File(getFilesDir(), generateFileName(params[0]));
try {
URL u = new URL(params[0]);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
视频播放方法:
public void playVideo(String fileName) {
Uri data = Uri.parse(new File(fileName).getAbsolutePath());
Log.i("DATA: ",""+data);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(data, "video/mp4");
startActivity(intent);
}
【问题讨论】:
-
尝试通过浏览器下载此视频并播放。如果它不起作用,看起来视图没有兼容格式。如果可行,请发布您的 readFully 方法
-
您的文件路径是否正确?
-
你成功了吗?你有哪个安卓版本?我有一个类似的问题。在 Android 4.2 (Samsung Galaxy S3) 上运行良好,但在 4.4.4 (Nexus 4) 上运行良好
标签: android android-video-player video-player