Tutorial / Android Development

Android Beginner: Introducing Media Player Class

The Full Series of Android App Developemnt

Welcome to the part 4 of “The Beginner’s Guide to Android Development“. Thank you all for following us. In this tutorial, we will describe the Media Player Class and using it to implement sound effects to a button. If you haven’t already, be sure to read the previous tutorial of this series first!

The source code we are going to use in today lesson is what we have working on the previous published post called “Android Beginner: Implementing Click Handler“. Let’s open up the source code in Eclipse editor and start our tutorial!

Introducing Android MediaPlayer Class

The Android MediaPlayer Class can be used to control playback of audio/video files and streams. With Android MediaPlayer Class, we can easily integrate audio, video into our applications. MediaPlayer Class supports several different media sources such as: local resources, internal URI or External URI.

Following code illustrates how we create a MediaPlayer object and play audio that available in our local resources:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); //create audio object
mediaPlayer.start(); // start to play audio

Step 1: Preparing Sound Effects

Ok, we have some rough idea on what MediaPlayer is, so is time for us to use it for our application. Before we start, we need to get a sound effects for our application, and here is the example of sound that we can download from Flashkit.com – beep.mp3.

Please create a new folder called “raw” under /res/ folder. Then place the mp3 file under /res/raw/ folder.

Step 2: Adding Sound Using MediaPlayer Class

Ok. let’s adding the sound to our application! Open up “myMainScreen.java” file and insert the following codes:

package com.tutorial.myapp;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class myMainScreen extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final MediaPlayer btnSound = MediaPlayer.create(myMainScreen.this, R.raw.beep);
		Button button1 = (Button) findViewById(R.id.sound_efx);
		button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnSound.start();
				startActivity(new Intent("com.tutorial.SOUND_EFFECTS_DETAILS"));
			}
		});
	}

}

Description

  • Line 5:
    Import the Android MediaPlayer framework.
  • Line 17:
    Declare a variable using keyword final*. We adding beep.mp3 to this variable using MediaPlayer Class. MediaPlayer Class create() method accepts 2 parameters: context and sound file.
  • Line 25:
    We play the beep.mp3 using start() method of MediaPlayer Class..

*Final Keyword

A java variable can be declared using the keyword final. Then the final variable can be assigned only once. Which means that, assigning the final modifier to a reference means it cannot change to point to another object.

Save all documents and run it on emulator. You should able to hear “beep” when you click on the button. :)

Conclusion

You’ve just learned about MediaPlayer Class. You know how to add sound to Android application. You also know to instruct the application to play sound effects which respond to user’s input. Stay tunes for upcoming tutorial.

  • Stumble it!
  • Share
  • Guest

    The first two lines, the ones before step 1, where do i put them?

    MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
    mediaPlayer.start();

  • waleed

    thanks for the article, it helped me in my project

    • onlymobilepro

      You’re welcome! Please do follow us to get the latest article :)