Playing sound on EVERY buttonclick(Android= - android

I am currently working on an application for Android and I'm using Eclipse and of course Android SDK, but I have bumped in to a problem that is almost the only thing I need to fix before I can relaese a beta-version to Android Market.
So, my problem is that I have an xml with 4 different buttons, and if the user press on a certain button, one sound will be played, and if the user press any of the other buttons is pressed another sound will play but the sound only play sometimes, I want it to play every time the user press a button.
Here's my code(concerning the mediaplayer):
public MediaPlayer right=null;
public MediaPlayer wrong=null;
if(right!=null) {
right.reset();
right.release();
}
if(wrong!=null) {
wrong.reset();
wrong.release();
}
right = MediaPlayer.create(getBaseContext(), R.raw.rightsound);
wrong = MediaPlayer.create(getBaseContext(), R.raw.wrongsound);
if(****()){
right.start();
}
else {
wrong.start();
}
That's my code and I would be very grateful if somebody could help me solve my problem.

new Thread() {
public void run() {
int sound = R.raw.wrongsound;
if(****()) {
sound = R.raw.rightsound;
}
mp = MediaPlayer.create(Test.this, sound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
}.start();

Related

android code acting differently on device than emulator

hi i developed a simple app that should play a sound if a boolean is true and then another sound when it completes or just play the second sound if the boolean is false and it all works perfectly on the avd but when run on device it doesint let the first sound complete before jumping to the second
this is in the onCreate
setContentView(screen);//pl
if(msg==true) {//boolean set from other activity
playInfo();
}else if(msg==false){
playSound();
}
}
this is the play info method which starts to play but after a second it jumps to playsound method on the phone but works perfectly on avd
private void playInfo(){
mp = MediaPlayer.create(this, R.raw.msg);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
msg = false;
playSound();
}
});
}
this is the playsound method
private void playSound() {
mp = MediaPlayer.create(this, song);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playSound();//loop the sound
}
});
}
again this works perfect on emulator but not on a device why would they act differently and any ideas on how it would be resolved?
The activity was set to landscape mode on the device this caused the activity to be redrawn very quickly after launching so it started got destroyed and started again with the boolean set to false however on the emulator this does not happen it just draws the activity once in landscape mode solved the problem by using 2 different mediaplayers and just play the first short sound over the other rather than rework the code.

Unable to play two MediaPlayer at same time in Nexus 5

I am trying to play some audio in background using two MediaPlayers inside a Service. Everything works fine if I want to play only one mp3, but If I try to play two at the same time, using two instances of MediaPlayer, only the last one works. It is weird because it works in emulator and some devices, but I am unable to make it work in a Nexus 5. This is the code I am using to create the two MediaPlayer instances:
private void playWithPiano() {
mp1 = initializePlayer(filenameVoz); // filenameVoz is the mp3 file name in raw folder
mp2 = initializePlayer("base_piano");
mp1.start();
mp2.start();
}
private MediaPlayer initializePlayer(String filename) {
int identifier = getResources().getIdentifier(filename,
"raw", getPackageName());
MediaPlayer mp = MediaPlayer.create(this, identifier);
mp.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d(TAG_LOG, "OnErrorListener");
stop();
return false;
}
});
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
return mp;
}
If I comment the mp2.start line, then I can listen the mp1 audio. It is like when a MediaPlayer is started, any other instances are stopped I would appreciate any help with this.
I found other posts describing the same problem in Nexus 5 devices, but don't have a solution yet.
https://code.google.com/p/android/issues/detail?id=63099
https://code.google.com/p/android/issues/detail?id=62304
I tried using seekTo(0) like it was suggested in one of those forums, but it didn't work, so I tried another thing that seems like an specific workaround for my case.
I am using this inside a Service, and my second MediaPlayer is only to play some background music, so I started it half second after the first one and it worked.
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
if (mp2 != null) mp2.start();
}
},
500
);
I still don't know why I am having this problem only in the Nexus 5, but I hope this may help somebody.

MediaPlayer.start() sound execution delayed

I've a strange problem with my app.
Every time a button is clicked, I call a method to execute sound.
The code:
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(context, R.raw.button_click);
}
mediaPlayer.start();
}
}
Where isSoundEnabled function verified if user has sound enabled inside app.
Sound is not executed each time I tap button, but after I tap other buttons, and sounds are executed all together.
So happen that I press three different buttons, and three sounds are executed only when I push a button for third time. How can I execute immediately the sound?
I have this problem on lollipop devices (the same code run perfectly on the same device but with kitkat)
Change your execute method to this
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(mContext, audioUriPath);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
}

mediaplayer gets hanged

My list consists of 12 songs and it has to move in an infinite loop when the button is clicked.my mediaplayer is working fine in emulator but it is getting stuck if i press the button continuously for 32 times in htc mobile.
public void onClick(View v)
{
if(count==listlen)
count=-1;
if(count<listlen)
{
count=count+1;
}
loadpitch(concatstr);
}
load pitch has this
try
{
if(sp.isChecked()||sm.isChecked())
{
mp.reset();
mp=MediaPlayer.create(this,resID);
mp.setLooping(true);
}
if(play==true)
{
mp.start();
}
}
where listlen is the length of the pre-defined list
loadpitch is function which loads the song
sp and sm are toggle buttons!
check this way for ur media player while starting media player onclick
if (mPlayer!=null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer= MediaPlayer.create(YourActivity.this,song);
mPlayer.start();

MediaPlayer.start() dies from too much clicking

In Adnroid, at first i declare the mediaplayer by
MediaPlayer mpl;
next I have this in the onCreate method
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.hit );
mp.setVolume(1, 1);
and a function that's supposed to play a sound when called
public void click()
{
mp.start();
}
yet the problem is that if the user calls this function multiple times, before it has stopped playing the last sound, it will die and stop playing any sounds, before the app is reset.
Any ideas how to fix that?
Thanks!
edit - found a solution:
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpl.release();
}
});
public void click()
{
if( ! mp.isPlaying() ) {
mp.start();
}//if
}//met
you can disable the button.
or you can stop current playing and star
new in onClick()

Categories

Resources