Maybe you are wondering how to play a MP3 sound by clicking a simple Button on Android.
Eh, you know what? You are in the right place!
You can see the result of this application directly on Android Market as Tuto 4 - PlayingSounds.
Note that this tutorial for MP3 sounds works as well with MP3 musics and of course a lot of different types of format such as WAVE or MIDI.
A complete list can be found on the official website of Android developpers:
http://developer.android.com/guide/appendix/media-formats.html.
After a presention of which MP3 we will need, we will show the source code needed for this tutorial.
First of all, we need .MP3 files: either created them or download them.
If you never created one, I suggest you to try a MP3 recorder such as Audacity.
But if you prefer to download them instead of created them, you have to find MP3 on Internet.
OK, we assume that we have now three MP3 files:
It is important to know that if the MP3 format, of files, is not 100% valid it won't work.
Now let's check the code.
We will select the Android 2.2 (API 8) version and create a new Android project called Tuto4_BadprogTutorialPlayingSounds.
In the code below, we will "talk" with button already on the main.xml file.
So we will use an event listener on each Button to trigger the correct sound.
For that we will create a manager of sound to check which Button is clicked.
Notice that the what.mp3 file will never (normally) be played.
Location: src/package/Tuto4_BadprogTutorialPlayingSoundsActivity.java
package com.badprog.android.tutorial.mediaplayer.playingsounds;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Tuto4_BadprogTutorialPlayingSoundsActivity extends Activity {
/**
* Variables
*/
MediaPlayer mp = null;
String hello = "Hello!";
String goodbye = "GoodBye!";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* Talking with the buttonHello
*/
final Button buttonHello = (Button) findViewById(R.id.idHello);
buttonHello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
managerOfSound(hello);
} // END onClick()
}); // END buttonHello
/**
* Talking with the buttonGoodBye
*/
final Button buttonGoodBye = (Button) findViewById(R.id.idGoodBye);
buttonGoodBye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
managerOfSound(goodbye);
} // END onClick()
}); // END buttonGoodBye
} // END onCreate()
/**
* Manager of Sounds
*/
protected void managerOfSound(String theText) {
if (mp != null) {
mp.reset();
mp.release();
}
if (theText == hello)
mp = MediaPlayer.create(this, R.raw.hello);
else if (theText == goodbye)
mp = MediaPlayer.create(this, R.raw.goodbye);
else
mp = MediaPlayer.create(this, R.raw.what);
mp.start();
}
}
Location: res/layout/main.xml
<?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/idHello"
android:text="@string/stringHello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/idGoodBye"
android:text="@string/stringGoodbye"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Location: res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Tuto4_BadprogTutorialPlayingSoundsActivity!</string>
<string name="app_name">Tuto 4 - PlayingSounds - BadproG.com</string>
<string name="stringHello">Hello</string>
<string name="stringGoodbye">Goodbye</string>
</resources>
We have to add a new directory named raw/ in the res/ one.
Location:
Launch your app and click the Hello Button to hear the hello.mp3 file and for the GoodBye one to hear the goodbye MP3.
Great job, you made it! ![]()
Comments
Anonymous (not verified)
Sunday, August 5, 2012 - 4:24pm
Permalink
This project code very useful
This project code very useful so thanks your sher Knowlage every one......
so plz help me pass one Activity but back onBackPassed(); method provious state here but ImageButton onClick event not working so any suggention that send me......
nani (not verified)
Wednesday, August 15, 2012 - 9:04am
Permalink
Thanks ..it's working fine
Thanks ..it's working fine
Gabriela (not verified)
Friday, February 8, 2013 - 10:44am
Permalink
Good this help me :) Thanks.
Good this help me :)
Thanks.
aditi (not verified)
Tuesday, April 23, 2013 - 11:11am
Permalink
good example thanku very much
good example thanku very much!!!
Add new comment