Posts Tagged ‘assets’

[Android] How to access media data in assets and res/raw folder

Monday, November 14th, 2011

To make custom media player, I tried to use MediaPlayer.
But when I set datasource, it could not work well.

I placed video files in assets/ or res/raw.
I didn’t know how to access.

I googled.
Then I found the solution.

If you placed in assets/video_file.mp4

_player = new MediaPlayer();
AssetFileDescriptor afd = getAssets().openFd("video_file.mp4");
_player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    		
_player.setDisplay(_holder); //_holder is SurfaceHolder of SurfaceView
_player.prepare();

Or if you placed in res/raw/video_file.mp4

_player = new MediaPlayer();
 String fileName = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
_player.setDataSource(this, Uri.parse(fileName));
    		
_player.setDisplay(_holder); //_holder is SurfaceHolder of SurfaceView
_player.prepare();

These pages have some very useful information.
Thanks.

Discovering Android – Embedding Video in an Android Application
Play audio file from the assets directory