我尝试使用VideoView播放下载的视频,但收到以下错误:
04-14 13:10:49.057: E/MediaPlayer(2229): error (1, -2147483648)
这是我下载视频的方式:
private String getDataSource(InputStream stream) throws IOException {
if (stream == null)
throw new RuntimeException("stream is null");
File temp;
if (!canSaveVideo) {
temp = File.createTempFile("mediaplayertmp", ".mp4");
temp.deleteOnExit();
}
else {
File dir = new File(localFilePath);
if (!dir.exists()) {
dir.mkdirs();
}
//localFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
temp = new File(localFilePath + "VID_" + localSenderID + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4");
}
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[256000];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
out.flush();
} while (true);
out.flush();
out.close();
/*
try {
stream.close();
} catch (IOException ex) {
Log.e("tag", "error: " + ex.getMessage(), ex);
}
*/
temp.setExecutable(true,false);
temp.setReadable(true,false);
temp.setWritable(true,false);
return tempPath;
}
以上代码从Socket.getInputStream()
下载视频文件并返回视频的绝对路径。它工作正常,因为我可以手动播放下载的视频文件。
但是,当我尝试使用VideoView播放下载的视频文件时,会抛出错误(1,-2147483648)。
这是我使用VideoView播放视频的方式:
private void playVideo(String videoPath) {
try {
videoView.setVideoPath(videoPath);
videoView.start();
videoView.requestFocus();
} catch (Exception e) {
Log.e("tag", "error: " + e.getMessage(), e);
if (videoView != null) {
videoView.stopPlayback();
}
}
}
我相信这一行:
videoView.setVideoPath(videoPath);
导致错误,因为当我在tempPath
方法中打印出getDataSource()
并将其直接传递给videoView
时,它起作用了:
videoView.setVideoPath("/sdcard/Surveillance/download/VID_KDuong_20140414_130853.mp4");
有谁知道如何解决这个问题?