我正在尝试在Android模拟器中播放视频我在我的资源文件夹中的视频以及原始文件夹但是在做了一些研究后仍然无法在我的模拟器中播放视频我正在使用android 2.1我的视频格式是mp4所以我不要认为这应该是一个问题任何人都可以给我一个示例代码,以便我可以了解更多?
问题是我需要显示视频的VideoView只会使用URI或文件路径指向视频.
如果我将视频保存在raw或assets文件夹中,我只能获得输入流或文件描述符,似乎没有什么可用于初始化VideoView.
更新
我仔细研究了MediaPlayer示例,并尝试使用FileDescriptor启动MediaPlayer到资源文件,如下面的代码所示:
SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);
player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Run Code Online (Sandbox Code Playgroud)
现在我得到以下异常:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
Run Code Online (Sandbox Code Playgroud)
似乎没有其他方法可以在启动时将文件复制到SD卡,这似乎浪费时间和内存.
我在/ res/raw /文件夹(/res/raw/textfile.txt)中有一个资源文件,我试图从我的Android应用程序中读取进行处理.
public static void main(String[] args) {
File file = new File("res/raw/textfile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
// Do something with file
Log.d("GAME", dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的路径语法但总是得到java.io.FileNotFoundException错误.如何访问/res/raw/textfile.txt进行处理?是File file = new File("res/raw/textfile.txt"); Android中的错误方法?
*答案: …