Is there a way I can tell if a certain sound is playing in SoundPool???
I.E.
if(MySound./*Command Goes Here*/()==true{
//Do stuff
}
I would appreciate if you could point me in the right direction or give me the code. I am not able to find any thing that does this on the docs.
This is not possible using SoundPool.
See Android SoundPool: get notified when end of played for two other ideas:
Use MediaPlayer, which provides an OnCompletionListener, instead of SoundPool
Use MediaPlayer in conjunction with SoundPool. Load the sound in a MediaPlayer beforehand to get the duration and keep track of whether the sound is done playing based its duration when played with SoundPool.
Related
I'm trying to use Android's MediaPlayer to play a backing audio track to a music game which should stay in sync with other events.
However, I'm finding that on different devices the backing track seems out of sync.
After some frustration, I'm coming to the conclusion that the time it takes to start playing the audio is indeterminate.
Is there a way around this? Or some kind of callback from the MediaPlayer that represents "starting to play NOW"?
AFAICT I can't use the SoundPool for this backing track as the audio file is too long.
I have loaded sounds using Soundpool in Android.
I have used the play function to play the sound.
How do I know how long a file is and where I am currently in that file as it is playing?
Sound File Duration
MediaPlayer is probably your best bet, here:
MediaPlayer player = MediaPlayer.create(ctxt, R.raw.mysound);
int duration = player.getDuration();
player.release(); //(If not using this player later on)
Current Playback Position
SoundPool does not support this functionality. Your three main options:
Use MediaPlayer for playback. MediaPlayer supplies a getCurrentPosition() method.
Use AudioTrack -- manually sending uncompressed audio to the stream on a byte-by-byte basis. AudioTrack has some basic functionality for determining playback position (usually accurate to within a few tens of milliseconds, on most devices), and besides that you can know exactly how much audio's been added to the playback buffer.
Stick with SoundPool, but time the playback yourself. That is, take a time hack (SystemClock.UptimeMillis()) or set a CountDownTimer, and estimate your playback position by assuming that playback began as soon as play() returned. This approach is rife with problems and inaccuracies, but has the benefit of not involving AudioTrack's complexity.
When using a SoundPool audio class, it definitely has some advantages over a MediaPlayer when just playing short audio clips. The two I've noticed is SoundPool is a lot faster. MediaPlayer can lag a bit on startup and it's much easier to go from one sound clip to the next, where I don't have to stop,reset,prepare each time.
However, is it possible to use the visualizer to get real time fft data from audio data playing in the SoundPool like it is for MediaPlayer? I couldn't come across any topics that cover that, but by the off chance I thought I would ask because it seems like it should be possible. The documentation says, "The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream." So if I could reference the MediaPlayer that SoundPool is using then I think I could just use getAudioSessionId() perhaps?
I already tried setting session ID to 0 to just get the output mix. It didn't work but it's not really the ideal effect that I am looking for anyway. Also one of the first things I did was try using the loaded SoundPool SoundID in place of the visualizers session ID, but that also didn't work.
I am using sound pool to play a short sound files in android, but I want to play the sounds at for example, If I clicked a button to play a sound and clicked the button again, I don't want to play the sound as long as it is already playing . how ?
It looks like you should use a MediaPlayer Class instead of soundpool to accomplish this result, in combination with an OnClickListener.
A simple StackOverflow search returns the following:
MediaPlayer
Maybe anybody faced with this issue.
I use SoundPool for playing sounds in my app.
After playing the sound I need to do further action. But how can I determine that the sound has stoped?
All the sounds in the app have a different length, so I can not count the time. I need a different approach.
Thank you.
soundpool's purpose is to load and play simple small sounds without the need to monitor them.
what you probably need is MediaPlayer and setOnCompletionListener() :
http://developer.android.com/reference/android/media/MediaPlayer.html
http://developer.android.com/reference/android/media/MediaPlayer.html#setOnCompletionListener(android.media.MediaPlayer.OnCompletionListener)