我正在尝试播放我在项目中保存的视频。我已下载this (.mp4测试视频)然后在项目的根目录中创建了一个名为vid的文件夹。然后我使用了这段代码:
public void PlayLocalVideo(View view)
{
VideoView video=(VideoView) findViewById(R.id.video1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setKeepScreenOn(true);
video.setVideoPath("android.resource://uk.co.SplashActivity/vid/big_buck_bunny.mp4");
video.start();
video.requestFocus();
}
我的xml看起来像这样:
<VideoView
android:id="@+id/video1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
PlayLocalVideo是我用于按钮上的onclick事件的方法。但是当我按下播放时没有任何反应:(
答案 0 :(得分:11)
只需将文件粘贴到 res / raw / big_buck_bunny.mp4 而不是vid文件夹并更改 您的videoPath:
video.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny);
答案 1 :(得分:1)
问题可能出在Android操作系统缺陷中,这使您无法正常访问超过1Mb大小的文件Load files bigger than 1M from assets folder
您可能需要将视频文件拆分为1Mb大小的部分。然后将这些部分合并到SD卡上的一个文件中并播放它。
例如,我已将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();
}
答案 2 :(得分:0)
试试这个代码......
首先在res目录中创建文件夹名称raw,将视频复制到该文件夹中并试用此代码...
video1=(VideoView)findViewById(R.id.myvideoview);
video1.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/"+R.raw.YOUR_VIDEO_FILE_NAME));
video1.setMediaController(new MediaController(this));
video1.requestFocus();
video1.start();