Exploring the Media Store Class


Build your own Android App Dev Empire

Get Instant Access

The MediaStore class provides an interface to the media that is stored on the device (in both internal and external storage). MediaStore also provides APIs for you to act on the media. These include mechanisms for you to search the device for specific types of media, intents for you to record audio and video to the store, ways for you to establish playlists, and more. Note that this class was part of the older SDKs, but it has been greatly improved with the 1.5 release.

Because this class supports intents for you to record audio and video, and the MediaRecorder class does also, an obvious question is, when do you use MediaStore vs. MediaRecorder? As you saw with the preceding video-capture example and with the audio-recording examples in Chapter 9, MediaRecorder allows you to set various options on the source of the recording. These options include the audio/video input source, video frame rate, video frame size, output formats, and so on. MediaStore does not provide this level of granularity, but you are not coupled directly to the MediaRecorder if you go through the MediaStore's intents. More important, content created with the MediaRecorder is not available to other applications that are looking at the media store. If you use MediaRecorder, you'll want to add the recording to the media store using the MediaStore APIs. To that end, let's see how we can leverage the MediaStore APIs.

If you recall from Chapter 9, recording audio was easy, but it gets much easier if you use an intent from the MediaStore. Listing 12-2 demonstrates how to use an intent to record audio.

Listing 12-2. Using an Intent to Record Audio import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class UsingMediaStoreActivity extends Activity { ^Override protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);


Button btn = (Button)findViewByld(R.id.recordBtn); btn.setOnClickListener(new OnClickListener(){

^Override public void onClick(View view) { startRecording();

public void startRecording() {

Intent intt = new Intent("android.provider.MediaStore.RECORD_SOUND"); startActivityForResult(intt, 0);

^Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) { case 0:

Uri recordedAudioPath = data.getData(); int i=0;

// record_audio.xml layout file <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:id="@+id/btn"

android:text="Record Audio" android:layout_width="wrap_content" android:layout_height="wrap_content" />


Listing 12-2 creates an intent requesting the system to begin recording audio. The code launches the intent against an activity by calling startActivityForResult(), passing the intent and the requestCode. When the requested activity completes, onActivityResult() is called with the requestCode. As shown in onActivityResult(), we look for a requestCode that matches the code that was passed to startActivityForResult() and then retrieve the URI of the saved media by calling data.getUri(). You could then feed the URI to an intent to listen to the recording if you wanted to. The UI for Listing 12-2 is shown in Figure 12-10.

9bd6 03:18 I F

Record your message

Message recorded


Was this article helpful?

0 0

Post a comment