Android : Play a video from a private application file
I'm using a videoView to display a video that I downloaded from my server.
If I play the video from the sd card, it works fine using something like :
video.setVideoPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.mp4");
But in my case, I don't want the video to be played from the sdcard but from the internal storage of the application. In this case, I don't find t开发者_如何学Che right path ... I thought it was something like :
video.setVideoPath("test.mp4");
or
video.setVideoPath("file://test.mp4");
or
video.setVideoPath("data/data/com.myapp/file/test.mp4");
But it doesn't work and the video view sends an error while trying to play the video.
Juste a precision, the file itself is well downloaded (in both ways), as I checked using the sd card and the file system (its size is ok). It seems to be the path given that is wrong .
Any idea ?
thx
If you want to play the video from the internal storage of the application, use the following lines of code :
MediaPlayer mMediaPlayer = new MediaPlayer();
File file = new File("data/data/com.myapp/file/test.mp4");
FileInputStream fis = new FileInputStream(file);
FileDescriptor fd = fis.getFD();
mMediaPlayer.setDataSource(fd);
This will surely help you.
精彩评论