Payday Loan Payday Loan

So, let’s continue with the logic of stopping a sound when is called a second sound from the soundboard. I will start directly by showing what you will need to write in the OnClickListener of every button, so you will not get a Force Close error.

ImageButton a=(ImageButton)findViewById(R.id.ImageButton01);
ImageButton b=(ImageButton)findViewById(R.id.ImageButton02);
a.setOnClickListener(new OnClickListener(){
	@Override
	public void onClick(View v) {
if(mp==null){playSound(R.raw.sound1);}//if is not playin any sound, then we start this sound
else{mp.release(); 
playSound(R.raw.sound1);} //else if there is a sound playing, we release it and then play the current sound
 
		}});
b.setOnClickListener(new OnClickListener(){
	@Override
	public void onClick(View v) {
		if(mp==null){playSound(R.raw.sound2);}
 
		else{
			mp.release();
		playSound(R.raw.sound2);}
		}});

Also, you can get an error when you press the hard key “back” on your phone, if the sound still playing. For this we override the finish() method like this:

@Override
		public void finish() {
		if(mp==null){}
		else{mp.release();}
			super.finish();
		}
||||| 0 I Like It! |||||

 
I will show you a function that allows you to play a sound in Android.
I implemented this function to be easier and just to call it at OnClickListener.
So the function is:

 public void playSound(String fileId) {
 
try {
 
mp.reset();
mp.setDataSource(fileId);
 
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
 
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
 }
});
} catch (Exception exception) {
}}

Now let’s say you want to create a soundboard:
we need to attach this function to a click listener of a Button.
Firstly you need to create a subfolder in the res folder named raw…Here you will put your sound…let’s say
you call it sound.mp3. So the code is:

Button x=(Button)findViewById(R.id.Button01);
x.setOnClickListener(new OnClickListener(){
 
 @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
playSound(R.raw.sound);
}});

Now i will show you how it looks whole program:

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.view.MenuItem;
import android.view.View;
 
import java.io.IOException;
 
public class SoundBoard extends Activity
{
@Override
public void onCreate(Bundle icicle)
{ super.onCreate(icicle);
setContentView(R.layout.soundboard);
Button x=(Button)findViewById(R.id.Button01);
x.setOnClickListener(new OnClickListener(){
 
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
playSound(R.raw.sound);
}});
 
}
public void playSound(String fileId) {
 
try {
mp.reset();
mp.setDataSource(fileId);
 
mp.prepare();
mp.start();
 
mp.setOnCompletionListener(new OnCompletionListener() {
 
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
 
}
});
} catch (Exception exception) {
}}
}

I will come back with completion of this tutorial…I will show you how to play and stop, and how to play a sound on pressing one button and how to stop current sound and play second sound on pressing on other button without force closing

 

||||| 2 I Like It! |||||

Search

Popular

Sponsors