Tuesday, June 14, 2011

Android: Mediaplayer prepare() method crashes.

If you tried to run voice recording with code from android developer site, you might have encountered this problem  -   prepare() method crashes. If you read logcat you can see that it is because of file permission problems. Device drive fails to create the file mentioned in the code.

Here is the work around:

Audio record is using a file that is not on the driver yet. So create a file named audio.3gp in your local file system. File can be empty.
On eclipse, open DDMS by Window> open perspective > DDMS
Open window > show view > File Explorer
Select data > local > temp
Click on 'push a file onto folder' button on the right top corner to bring up file select dialog.
Select audio.3gp  created in the local file system. It will be added to data > local > temp in the device.

Change name of the file in code to "data/local/tmp/audio.3gp"
Now device driver can see the file and your project should run fine.