Android, load sounds and play immediately? - android

I have been attempting to play a bunch of sounds in my app as some views switch. The sound is narrative for the views and is between 3 and 10 seconds each, of which there are about 30.
First, I tried loading them all into a sound pool and passing it to a hashmap on program load, then I simply play them when required. This worked for the first 5 or 6 sounds but once I started to add more sounds to the sound pool, the later ones did not play. I assume this is due to the 1MB limit I have read about on soundpool.
I tried switching to just loading the sound and passing it straight to play on the next line however, no sounds play. Logcat just shows a reset and command cancelled for the player.
I switched then to loading the file and pointing to it with the hash map, however, even after doing an unload and loadimg a new sound at the same index it would just play the same sound every time.
I have tried MediaPlayer but it is ineffective for my desired application.
Can anyone suggest a way I should look to implement this properly? And should I be trying to load all sounds before hand or not?

I think you need to wait for the load to complete before you can play it. Add an onLoadCompleteListener and then play it when that is invoked.

Related

Small pause between looping MediaPlayer

I'm trying to loop a ~30 seconds audio track multiple times. I'm using a MediaPlayer to play the sounds, here is the code where its created:
mAnimalMediaPlayer = MediaPlayer.create(mContext, fish.getSound());
I then call:
mAnimalMediaPlayer.setLooping(true);
mAnimalMediaPlayer.start();
when a play button is clicked. When the track loops, there is a small but noticeable pause between the track ending and the new loop beginning. I am positive that the audio file im using does not contain any actual pause.
Here is an android bug report which has been up for some time: https://code.google.com/p/android/issues/detail?id=18756
I've tried a few workarounds (such as using .ogg files instead of .mp3) but the problem persists.
I'm looking for any possible workaround that will allow me to seamlessly loop the playback multiple times.
There is a very noticeable pause. However, I have just discovered through experimentation on an emulator running level 25 that if one prepares two identical players and alternately starts them one after the other when the other has finished then the gap becomes much more difficult to hear.

How to play multiple sounds simultaneously in Android?

I've been trying to make multiple sounds work simultaneously but I kept getting errors like Sample not ready and Error creating AudioTrack. Though I've taken care of the first error, the other error persists.
I'm using two mp3 files, that are away above 5 mb(>4 minutes) and thus was wondering if SoundPool is the right way to go about it? I can use Media Manager but it doesn't let me play multiple files simultaneously.
SoundPool is recommended for <1MB sound clips, so I'd use MediaPlayer in your case. You can call the following for each sound and they will play simultaneously:
MediaPlayer.create(YourActivity.this, R.raw.your_sound).start();
Or you can create multiple MediaPlayer objects and play a sound on each, then release() them.

Playing Multiple Sounds in Android

I want to Play Multiple sounds. I know how to play them, but i want to play them synchronously. For example i have all my sounds of length 4 second. if i play first sound at 04:00:00 and second play on 04:00:02 then the second one start on 04:00:04 not in middle.
Is there any framework available in android to do this Or any other Help...

How do I stop the sound restarting when a collision occurs

Currently my code plays a sound when there is contact between any objects in my game using andEngine and Box2D and the walls, the problem I have is that when contact is made with any objects it starts again I understand why this is happening. What I want to do if possible is keep playing the sound while also playing it for another collision. I think I may need to use threads but I am unsure how to do this in java for android.
#Override
public void beginContact(Contact contact)
{
Rattle.this.mExplosionSound.play();
}
UPDATE: I don't seem to have been able to fix the issue but I know that what I need to do is play this sound multiple times simultaneously, I have tried threads and the soundPool but got no luck with either still not sure what to do.
It seems you have only one instance of the sound. Then you call play on the same sound twice, therefore it stops playing and starts from the beginning.
Imagine having one CD player - you press play, the music starts to play. You press play again and the player starts reading the track from the beginning. What you need is two players, therefore you may need two instances of the same sound.
(I don't have much experience with sound on Android so I may be wrong, please take the advice with a grain of salt.)
Even I was developing a game some time back and I found a similar problem. You should use SoundPool to play short sounds like crashing and collisions. here are some good linkswhich helped me. Link 1, link 2.

Using Eclipse, Sound is not playing fully on emulator? Sound bytes too long?

I am currently trying to make a Soundboard application for my Android.
Using Eclipse, I have successfully made buttons and coded them so that when pressed, they play .ogg sounds in the res/raw folder. All is okay.
Now, I have discovered that any files over about 6 seconds long aren't playing fully.
Is there any way to correct this problem? I would normally just trim the files, but some of them need to be a bit longer, but none are over 50 seconds.
Thanks alot!
What is happening is the mediaPlayer.start() instruction is being called before the file has finished fully loading. Assuming you are using mediaPlayer.create(this, resid) followed by .start() there are a few things that can help. The "right" way to do it is to put the .start() instruction inside of an onPreparedListener since .create calls .onPrepared the first time, but honestly when I put that in my soundboards I got tons of crash reports, so I reverted to just using .start after .create.
I found the problem usually occurs if the sound file is very high bitrate, or if the app is installed on a slow sdcard. I use mp3's, but usually cap the bitrate at 96k and put in the app description that if the clips aren't playing fully to move the app back to internal memory and the complaints seem to have stopped.

Categories

Resources