Using the BuiltIn Audio Player via an Intent

As with using the camera, the easiest way to provide the ability to play an audio file within an application is to leverage the capabilities of the built-in "Music" application. This application plays all of the formats that Android supports, has a familiar interface to the user, and can be triggered to play a specific file via an intent.

The generic android.content.Intent.ACTION_VIEW intent with the data set to a Uri to the audio file and the MIME type specified allows Android to pick the appropriate application for playback. This should be the Music application, but the user may be presented with other options if he or she has other audio playback software installed.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); intent.setDataAndType(audioFileUri, "audio/mp3"); startActivity(intent);

NOTE: MIME stands for Multipurpose Internet Mail Extensions. It was originally specified to help e-mail clients send and receive attachments. Its use, though, has extended greatly beyond email to many other communication protocols, including HTTP or standard web serving. Android uses MIME types when resolving an intent, specifically to help determine which application should handle the intent.

Each file type has a specific (sometimes more than one) MIME type. This type is specified using at least two parts with slashes between them. The first is the more generic type, such as "audio." The second part is the more specific type, such as "mpeg." A generic type "audio" and a more specific type "mpeg" would yield a MIME type string of "audio/mpeg," which is the MIME type typically used for MP3 files.

Here is a full example of triggering the built-in audio player application through an intent: package com.apress.proandroidmedia.ch5.intentaudioplayer;



import android.content.Intent;


import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

Our activity will be listening for a Button to be pressed before it triggers the playback of the audio. It implements OnClickListener so that it can respond.

public class AudioPlayer extends Activity implements OnClickListener { Button playButton; ^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main);

After we set the content view to our XML, we can get a reference to our Button in code and set our activity (this) to be the OnClickListener.

playButton = (Button) this.findViewById(; playButton.setOnClickListener(this);

When our Button is clicked, the onClick method is called. In this method, we construct the intent with a generic android.content.Intent.ACTION_VIEW and then create a File object that is a reference to an audio file that exists on the SD card. In this case, the audio file is one that is manually placed on the SD card in the "Music" directory, which is the standard location for music-related audio files.

public void onClick(View v) {

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);

File sdcard = Environment.getExternalStorageDirectory();

File audioFile = new File(sdcard.getPath() + "/Music/goodmorningandroid.mp3");

Next, we set the data of the intent to be a Uri derived from the audio file and the type to be its MIME type, audio/mp3. Finally, we trigger the built-in application to launch via the startActivity call passing in our intent. Figure 5-1 shows the built-in application playing the audio file.

intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3"); startActivity(intent);

Here is a simple Layout XML file specifying the Button with the text "Play Audio" to be used with the foregoing activity.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:text="Play Audio" android:id="@+id/Button01"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>

SBDO 9:55am

0 Unknown artist

Q sdcard

RS goodmorningandroid

Q sdcard

RS goodmorningandroid

Figure 5-1. Android's built-in music player playing an audio file specified via an intent

+2 -3


  • prisca
    How to pick mp3 file via intent?
    2 years ago

Post a comment