我正在研究在循环中播放视频文件的应用程序。到目前为止,我只是安装设备并将视频文件复制到SD卡上,然后使用文件路径在VideoView上启动它。我正在尝试实施一种方法,可以远程更新播放的视频,因此我已经转而在线存储视频。在应用程序内部,我检查本地副本并下载(如果它不存在),或者如果有更新的副本。我已经在两个不同的视频文件上进行了测试.mp4s。下载其中一个播放第一次,但试图重新开始循环时,它告诉我无法播放视频。另一个甚至不会第一次播放,它只是给我一个表示视频无法播放的对话。如果我通过USB电缆将它们复制到SD卡上,这两个文件都可以正常使用我的应用程序。他们工作,如果我退出我的应用程序,并手动下载他们的东西(Dropbox),但不是如果我从我的应用程序内下载它们。下面是我使用的下载文件的代码:在线下载Android mp4视频下载时出现损坏
public static void DownloadFromUrl(String fileName) { //this is the downloader method
try {
URL url = new URL("http://dl.dropbox.com/u/myfile.mp4");
File file = new File(PATH + fileName);
long startTime = System.currentTimeMillis();
Log.d(myTag, "download begining");
Log.d(myTag, "download url:" + url);
Log.d(myTag, "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
Log.i(myTag, "Opened Connection");
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Log.i(myTag, "Got InputStream and BufferedInputStream");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
int current = 0;
Log.i(myTag, "About to write");
while ((current = bis.read()) != -1) {
bos.write(current);
}
fos.close();
Log.d(myTag, "download ready in"
+ ((System.currentTimeMillis() - startTime))
+ " sec");
} catch (IOException e) {
Log.d(myTag, "Error: " + e);
}
}
我知道在这个片段保管箱URL位置不正确,我改变它只是为这个职位,我的应用程序正确的网址指向的文件。创建文件时使用的变量PATH被设置在我的代码片段之外。
是否有关于此代码段可能会损坏我的mp4文件?
尝试使用您的方法将视频下载到SD卡上,然后使用USB电缆将文件从SD卡上拔下。看看这个文件是否播放。 – 2011-03-15 19:17:28
我没有在我的电脑上播放.mp4的播放器。我怀疑它不会,虽然我看着文件属性,我的方法正在下载的文件属性是比它们应该小的几个字节。 – FoamyGuy 2011-03-15 19:45:26