Tuesday, June 2, 2009

Android Permissions and Audio

Found a list of all possible permissions:

http://developer.android.com/reference/android/Manifest.permission.html

would be nice if when you were trying to do something without permission it would tell you which one was missing.

Given that the example MediaRecorder code in the Android docs didn't work I was forced to search the Google Android Group for tips, and found this post with code that worked.

The challenge was then to see if the sound was being stored and I eventually found that it was when I pulled the sound file off Android with (of course I had to get the eclipse emulator to load the relevant sdcard by adjusting the launch configuration first):

./adb pull /sdcard/test.3gpp test.mp3

again the emulator seems to be available for attachment randomly ... usually the following returns nothing:

samuel-josephs-computer-2:tools samueljoseph$ ./adb devices
List of devices attached
emulator-5554    device

I did find a good summary of SD Card creation for Android.  The next challenge was to load the sound file back from the file system in order to play it, and I worked out how to store the file in the applications file space rather than within the sd card.  I am now storing the file in a sub-directory of the application space and reloading in an activity context like this (excluding all the exception handling):

String filename = "test.3gpp";
File dir = this.getDir("sounds", MODE_WORLD_READABLE);
File file = new File(dir, filename);
FileInputStream is = new FileInputStream(file);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(fileDescriptor);
mMediaPlayer.prepare();
mMediaPlayer.start();

So that was pleasantly simple.