mediaplayer one sound at time - android

i'm newbie in this world of programming, i want reproduce one sound at time, is a application with long sounds of animals.
when i push some button make sound about some animals (gato make sound of a cat)
i fail to play a sound at time, i try with soundpool too, same results, obviously the problem are my low lvl of programming xD, can somebody help me?
public class MainActivity extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void presionGato(View v) {
mp.stop();
mp.create(this, R.raw.gato);
mp.start();
}
public void presionLeon(View v) {
mp.stop();
mp.create(this, R.raw.leon);
mp.start();
}
public void presionPerro(View v) {
mp.stop();
mp = mp.create (this, R.raw.perro);
mp.start();
}
public void presionTigre(View v) {
mp = mp.create (this, R.raw.tigre);
mp.start();
}
public void presionRata(View v) {
mp = mp.create (this, R.raw.rata);
mp.start();
}
public void presionSapo(View v) {
mp = mp.create (this, R.raw.sapo);
mp.start();
}
public void presionRana(View v) {
mp = mp.create (this, R.raw.rana);
mp.start();
}
}

I suggest to you, learn programming and search about it. You didn't listen anything from UI, how can you know which button was clicked?
first, initialize UI element and media player on onCreate;
Button gatoButton = (Button) findViewById(R.id.gato);
Button leonButton = (Button) findViewById(R.id.leon);
mp = new MediaPlayer();
then set onClickListener for each button.
gatoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionGato();
}});
leonButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
presionLeon();
}});

You can use
if(mp.isPlaying()){
mp.stop();
mp.release();
}
it will stop the music if it is playing then you can start the your new music again. It prevent to play the music at the same time.

Related

can't start the MediaPlayer again after stop method of MediaPlayeris called

In this application I have 2 buttons one is for start the media player and another one is for stop the media player ,But in my case stat and stop is working fine .
I can't start the media player once the stop button is clicked and also I got
start called in state 0, mPlayer(0x9f5d8300)
error (-38, 0)
I tried some solutions from stackoverflow but nothing is helped .
Coding :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
can anyone help me to fix it .
If I read the doc they say you need to call prepare() method to reset your player.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStop = (Button) findViewById(R.id.mStop);
mStart = (Button) findViewById(R.id.mStart);
final MediaPlayer player = MediaPlayer.create(this, R.raw.alarmsong);
mStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
player.setLooping(true);
player.setVolume(100, 100);
}
});
mStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
player.prepare();
}
});
}
you need to do
player.release();
after stopping media player

Is this a good way to use MediaPlayer?

I think this is a decent way to use MediaPlayer for a Button or one time use. Am I right? Is the try block necessary? And what should I be trying to catch here? I'm really having a hard time finding a rock solid way to play a sound once.
// Button sound
private void playButtonSound() {
try{
final MediaPlayer startPlayer = MediaPlayer.create(
getApplicationContext(), R.raw.sound);
startPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
startPlayer.release();
}
});
startPlayer.start();
} catch(Throwable t){}
}
I use it like that:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.tosse);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}

How to disable and enable layout according to media player in android

First I am using soundpool But with soundpool its difficult to do this task. So I am now using media player. In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish so that user able to play sound again when its finish.
Code-
private LinearLayout layout1;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
});
}
private LinearLayout layout1;
MediaPlayer mp;
boolean flag=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy);
mp = MediaPlayer.create(this, R.raw.test);
layout1 = (LinearLayout) findViewById (R.id.layout1);
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(!flag){
mp.start();
layout1.setEnabled(false);
} else{flag =true ;}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
flag=false;
layout1.setEnabled(true);
}
}); }
EDIT
You can do like this in onClickListener:
boolean flag;
layout1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if (flag == false){
flage=true;
layout1.setEnabled(false);
mp.start();
}
});
The value of flag is dependant on state of sound file.
The callback public void onCompletion(MediaPlayer mp) gives you a reference to the MediaPlayer.
public void onCompletion(MediaPlayer mp) {
flag = false;
layout1.setEnabled(true);
}

Stopping several Media Players in android

I have several MediaPlayers in the same activity. It works this way:
MediaPlayer mpOne starts playing, if a variable has a value "yes" MediaPlayer mptwo starts playing, else mpThree starts.
When the audio ends, starts another process identical
This is my code:
MediaPlayer mpOne,mpTwo,mpThree,mpFour,mpFive
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morning);
mpOne= MediaPlayer.create(this, R.raw.one);
mphora.setLooping(false);
mpTwo= MediaPlayer.create(this, R.raw.two);
mphora.setLooping(false);
mpThree= MediaPlayer.create(this, R.raw.three);
mphora.setLooping(false);
mpFour= MediaPlayer.create(this, R.raw.four);
mphora.setLooping(false);
mpFive= MediaPlayer.create(this, R.raw.five);
mphora.setLooping(false);
mpOne.start();
mpOne.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
if (variable.equals("yes"){
mpTwo.start();
} else {
mpThree.start();
}
});
mpTwo.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpThree.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpFour.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
// same proccess
});
}
My problem is when I want to stop all audios.
Currently I have a button that does the following:
stop.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(mpOne.isPlaying()){mpOne.stop();mpOne.release();}
else if(mpTwo.isPlaying()){mpTwo.stop();mpTwo.release();}
else if(mpThree.isPlaying()){mpThree.stop();mpThree.release();}
else if(mpFour.isPlaying()){mpFour.stop();mpFour.release();}
else if(mpFive.isPlaying()){mpFive.stop();mpFive.release();}
}
}
});
The problem is that sometimes it works and sometimes the audio still playing.
I've searched and found similar problems, but not a solution
Can anyone tell me a safe way to stop all playback?
Thank you very much in advance.
regards
You can do only one MediaPlayer instance and at onCompletion setting a different audio source

play the same sound in android without waiting for the previous to end

guys scenario is that suppose i click a button, the sound plays and within the duration of that track i again click the button and want to play it from the beginning. i tried with the following code, but no success.
code is :
public class SoundtestActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.gun_shot);
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
mp.reset();
mp.start();
}
}));
}
}
Try this code
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp.start();
}
});
Or replace mp.start(); by mp.reset(); in this code
Best way and the easiest way is =
mp.setLooping(true);
And yes it does work, just simply add this line after mp.start() and nothinfg else is required
You just need to comment the following lines and shift your Media player instance in the button's listener method, bingo your done with multiple media players playing parallel to each other
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(this, R.raw.gun_shot); // ADD THIS LINE HERE
if(mp.isPlaying()){
mp.stop(); //ADDED TO STOP FIRST
mp.release(); //ADDED TO RELEASE RESOURCES
}
mp.start();
}
}));

Categories

Resources