问题可能出在 Android OS 缺陷,它不允许您正常访问超过 1Mb 大小的文件 从 assets 文件夹加载大于 1M 的文件
您可能需要将视频文件拆分为 1Mb 大小的部分。然后将这些部分合并到sdcard上的一个文件中并播放。
例如,我已经big_buck_bunny.mp4
分成 5 个部分big_buck_bunny.mp4.part0
,big_buck_bunny.mp4.part1
依此类推。要合并它们,您可以使用此方法
private void copyVideoFromAssets(String inFilePrefix, String outFileName) throws IOException {
// Get list of files in assets and sort them
final String[] assetsFiles = getAssets().list("");
Arrays.sort(assetsFiles);
// Open the empty file as the output stream
final OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024 * 128];
for (String file: assetsFiles) {
if (file.startsWith(inFilePrefix)) {
// Open part of file stored in assets as the input stream
final InputStream input = getAssets().open(file);
// Transfer bytes from the input file to the output file
int length = input.read(buffer);
while (length > 0) {
output.write(buffer, 0, length);
length = input.read(buffer);
}
input.close();
}
}
// Close the streams
output.flush();
output.close();
}
public void PlayLocalVideo(View view)
try {
copyVideoFromAssets("big_buck_bunny.mp4.part", "/mnt/sdcard/big_buck_bunny.mp4");
} catch (IOException e) {
e.printStackTrace();
}
VideoView video=(VideoView) findViewById(R.id.video);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setKeepScreenOn(true);
video.setVideoPath("/mnt/sdcard/big_buck_bunny.mp4");
video.start();
video.requestFocus();
}