Android – Audio recording NullPointerException

android

I've seen several examples of this but they seem to be for older versions of the SDK. I'm trying to setup basic audio recording and the following code is giving me a NullPointerException when targeting version 2.0 of the SDK.

ContentValues values = new ContentValues(2);
values.put(MediaStore.MediaColumns.TITLE, "somename");
values.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis());

ContentResolver resolver = getContentResolver();
Uri base = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Uri newUri = resolver.insert(base, values);

I've narrowed it down to the last line as being what is throwing the exception. I've tested with the Log class and have found that the base and values variables are being set correctly.

Here is the exception output.

12-27 11:08:18.608: ERROR/DatabaseUtils(197): Writing exception to parcel
12-27 11:08:18.608: ERROR/DatabaseUtils(197): java.lang.NullPointerException
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:1478)
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1370)
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:150)
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:140)
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at android.os.Binder.execTransact(Binder.java:287)
12-27 11:08:18.608: ERROR/DatabaseUtils(197):     at dalvik.system.NativeStart.run(Native Method)

Also, in a lot of the examples I have seen, they add the mime type to the ContentValues object using something like

values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType());

where the recorder variable is a MediaRecorder object. It appears however that the getMimeContentType() method no longer exists. Could the insert be throwing the exception because I am not setting the mime type column? If so, How can I get the mime type in the new version of the sdk?

Thanks in advance!

EDIT:

I Think I found how to set the content mime type for the insert call.
the line I came up with looks like

values.put(MediaStore.MediaColumns.MIME_TYPE, MediaStore.Audio.Media.CONTENT_TYPE);

I do however still get a NullPointerException

Best Answer

Oddly, I hadn't seen the audio recording example on the Android developers' site before, involving all the ContentProvider stuff.

If all you need is to record audio to a file, then you don't need to do most of that; you can just set up the MediaRecorder object, point it to a file and start recording:


// Prepare recorder source and type
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

// File to which audio should be recorded
File outputFile = getFileStreamPath("output.amr");
Uri target = Uri.parse(outputFile.getAbsolutePath());
recorder.setOutputFile(target);

// Get ready!
recorder.prepare();

// Start recording
recorder.start();

// Stop and tidy up
recorder.stop();
recorder.release();
Related Topic