Inserting Audio into the Media Store

Appportunity

Build your own Android App Dev Empire

Get Instant Access

Audio recordings may be put into the MediaStore content provider so they are available to other applications. The process is very similar to the process we used earlier to add images to the MediaStore. In this case though, we'll add them after they are created.

We create a ContentValues object to hold the data that we'll insert into the MediaStore. A ContentValues object is made up of a series of key/value pairs. The keys that may be used are defined as constants in the MediaStore.Audio.Media class (and those classes it inherits from).

The MediaStore.Audio.Media.DATA constant is the key for the path to the recorded file. It is the only required pair in order to insert the file into the MediaStore.

To do the actual insert into the MediaStore, we use the insert method on a ContentResolver object with the Uri to the table for audio files on the SD card and the ContentValues object containing the data. The Uri is defined as a constant in MediaStore.Audio.Media named EXTERNAL_CONTENT_URI.

Here is a snippet that may be plugged into the CustomRecorder example just after the release method is called on the MediaRecorder (recorder.release()). It will cause the recording to be inserted into the MediaStore and made available to other applications that use the MediaStore for finding audio to play back.

ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.MediaColumns.TITLE, "This Isn't Music"); contentValues.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis()); contentValues.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath()); Uri newUri = getContentResolver().insert( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues);

Of course, in order to use the foregoing snippet, we'll need to add these imports:

import android.content.ContentValues;

import android.net.Uri;

import android.provider.MediaStore;

Was this article helpful?

+1 0

Responses

  • Alessia
    How to insert audio file in media using contentvalues?
    8 years ago

Post a comment