Weird Sound Looping on Click - android

I simply have a sound that executes on a button click. Everything works fine in the emulator but sound #2 plays in an infinite loop when I run on my phone.
Could this be a phone issue I have downloaded other apps from the market that have had these weird sound loops. (I have a Droid 2)
Declarations:
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.sound1);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound2);
Button 1 code is as follows.
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp1.start();
textView1.setText("Hello");
}
});
Button 2 code is as follows.(This is the loopy one)
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++;
mp2.start();
iv1.setVisibility(View.VISIBLE);
if (count == 5) {
//I do some enabling of components in here
}
}
});
I have tried
.stop()
.setLooping()
.pause()
as well as moving .start() after and before the if statement
All of these result in it still looping or no sound at all.

If you swap R.raw.sound1 with R.raw.sound2 does the looping problem stay with the 2nd media player or does it follow sound2?
I'm not an expert on the format of sound files, but it could be a problem with the file itself. Try opening the file in Audacity and cutting off the last 10 ms then save it as something new and see if the problem exists in the new file.

Try to add OnCompletionListener for this MediaPlayers with
mp.stop();
http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
Hope, it help you

Related

Play random sound on button click

I want to my application play random sound when user click on button. So I don't want android natural sound, I want to play my own sound every time user click on one button. Does anyone knows the solution of this problem.
To play a sound:
Play sound on button click android
For a random sound you just need to add all sounds in a list.
And in the onClickListener just get a random sound from your list.
Something like this:
List<Integer> soundList = new ArrayList<Integer>();
soundList.add(R.raw.sound1);
soundList.add(R.raw.sound2);
soundList.add(R.raw.sound3);
soundList.add(R.raw.sound4);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View v) {
playRandomSound();
}
});
private void playRandomSound() {
int randomInt = (new Random().nextInt(soundList.size()));
int sound = soundList.get(randomInt);
MediaPlayer mp = MediaPlayer.create(this, sound);
mp.start();
}
No guarantee that this works! It's just an example, how you could do it!
One way to achieve that is:
Embed your sound , as an MP3 encoded file
Attaching a click event handler in the Android App
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code that runs when button is clicked
}
});
Access and play the embedded MP3 file from the application in the event handler
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
see
How do I play an mp3 in the res/raw folder of my android app?
A tutorial explaining a complete process
http://www.accelerated-ideas.com/news/android-app-development--how-to-add-and-play-music-and-audio-files.aspx
Edit:
To disable native android click sound
yourbutton.setSoundEffectsEnabled(false);
source :
Disable Button click sound in android

Start and Stop a MP3 File with Two Buttons - Possible?

I have an mp3 file that I would like to start with one button but stop with another. Is this possible? Here is my code to start the file on button1 - How would I stop the file on button2?
Thank you!
button1.setOnClickListener(new View.OnClickListener() {
private MediaPlayer mplay;
public void onClick(View v) {
if (mplay == null){
mplay = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
}
mp.start();
}
});
Define your MediaPlayer outside of your ClickListener -- as a class variable -- then handle starting and stopping in the click handling of your buttons, as they'll both be able to see it.

Android the fastest way to load and play sound in application

I'm working on a project in which I have to load 6 different sounds in one activity and play all sound on button click.The sound file are not so big,but the problem is that maybe they will be more.So my question is which is the fastest way to load sound files in a single activity.For test purposes I used res/raw folder to store the sound files and tried with two different method to play the files,but the result did not satisfied me.Here is the two different types of code :
Method 1:
Button first = (Button) findViewById(R.id.one);
Button second = (Button) findViewById(R.id.two);
first.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumtwo);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
second.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(SoundFXActivity.this, R.raw.drumone);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
Method 2:
private SoundPool spool;
private int soundID,soundID2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(this, R.raw.drumone, 1);
soundID2 = spool.load(this, R.raw.drumtwo, 1);
Button three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sound();
}
});
}
public void Sound(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
But the problem is that the both methods are slow..I need to find a way a faster way to play the sounds..but now there is like almost a second after I click the button to play the sound.
Can you give me some advice how to play the sounda faster.Should I load them into a cache when the application starts or save them in database or somewhere else (I don't think database is a good option as a matter of fact,but I want to hear some suggestions).Or maybe load them from assets folder,but I think it still going to be slow.
So any ideas or suggestions?
Thanks in advance!
You could create the media players in your onCreate and then the buttons will just make them play. That would be the easiest solution, I would say.
Alternatively, depending on how important it is to you and how much work you want to do, you could consider using JetPlayer:
here's android development page for media:
http://developer.android.com/guide/topics/media/index.html
The page on the JetPlayer class:
http://developer.android.com/reference/android/media/JetPlayer.html
and the page on creating the JET files:
http://developer.android.com/guide/topics/media/jet/jetcreator_manual.html
If you implemented this it would likely work best but would definitely be the most work.
JetPlayer basically lets you have one audio file (i think it's a MIDI file) with multiple tracks that you mute and unmute as you please. I have no personal experience with it and have just read the docs some, but it seems like it would be very useful for any situation with more than one sound.
Edit: Also, it's worth mentioning this mailing list and in case the link ever changes this google search in case anyone is interested in android audio topics.
If you want to have the less delay you must use OpenSL in c++, From jelly bean is the faster sound player
SoundPool is working fine, you can see an example with the Hexiano project. However, you must preload the sounds into SoundPool, because it takes a lot of time to decode the file and store them in-memory before being able to use them. But once the sounds are loaded, there's no noticeable delay between key press and sound output.

MediaPlayer force close after consecutive clicks

I've made a button and when you click it, it gives away a short sound(Max one second-sound). But after I've clicked the button about 20 times in a row I get force close..
The code is:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.buzzer);
mp.start();
}
});
I've tried with mp.stop(); but then my sound stops after it have been played half of the time...
One more thing, does anyone know how to "prepare" the sound when I click? Because the sound gets delayed with some milliseconds the first time I press the button.
Create a MediaPlayer member variable and initialize it in onCreate() the same way you are doing in the listener. Then in the listener just use this code:
if(mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.start();
Then call mPlayer.release() in your finish() Activity. My guess is that since none of your MediaPlayer instances are being released, it's running out of memory to use.
The official document for MediaPlayer is actually incredibly descriptive and helpful:
http://developer.android.com/reference/android/media/MediaPlayer.html

Play sound file on Timer

I am displaying a Timer on a screen.
The requirement is I need to play a sound every second when the timer duration is 5 seconds remaining & finally when it reaches 0 (end) , I need to play another sound.
I am using the following code:
public void onTick(long millisUntilFinished) {
long timeLeft = secondsRemaining // I am getting the seconds left here
if (timeLeft <= 5) {
playAlertSound(R.raw.beep);
}
else if(timeLeft == 0){
playAlertSound(R.raw.beep1);
}
public void playAlertSound(int sound) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
The problem I am facing using the above code , though the each beep sound duration is less than 1 second , I am getting a continuation of the sound. I want to have separate beep sound for every second starting from the remaining 5 seconds till it reaches zero.
Also , the volume of the sound is too low.
Kindly provide your inputs , whether I need to follow any other logic ?
Thanks in advance.
Warm Regards,
CB
You should make sure looping is disabled using the setLooping function and for the volume issue, you can set the playback volume using: the setVolume function.
But, for your appliction you may want to look into using the SoundPool class instead of the MediaPlayer because it's more suitable for situations like yours when you want to play the same short sound more than once.
With your sample code, I would also reverse the order of the setting the onCompleteListener and starting playback, like this:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
// move the mp.start() from here..
mp.setOnCompletionListener(new OnCompletionListener() {
// your handler logic here..
});
// and add the setLooping and setVolume calls here..
mp.setLooping(false);
mp.setVolume(1.0, 1.0);
mp.start(); // to here..
I tried this code without the OnCompletionListener, and it worked fine with no looping? I guess I'm not understanding your problem. Also... setVolume(1.0,1.0) isn't correct, you need (float,float) not doubles.

Categories

Resources