[android-developers] Re: Playing sounds multiple times using MediaPlayer
Use OGG insteaed of WAVE. You'll find that it is lower overhead and latency. We will have better audio support in a future SDK. On Nov 16, 1:20 pm, Cool Frood [EMAIL PROTECTED] wrote: Hi, What is the length of the files that you are trying to play? I'm trying to do the same with 0.2s long WAV files, but I just can't MediaPlayer to behave well. At first I tried 1 MediaPlayer, but it was too laggy. In my application, I'm trying to play the sound about 75 times a minute, that is, once every 0.8 seconds. At this rate, there should be plenty of time (0.6 s) between invocations, but even with a pool of 10 MediaPlayers, things don't work very well. Like Robert Green said in this thread earlier, MediaPlayer is just unusable for short, repeated sounds. How has your experience been so far? On Nov 2, 11:20 am, tobias429 [EMAIL PROTECTED] wrote: Thanks for the tip! This actually does the trick. What I'm doing now is a bit more complex. I've created a helper class that multiplies the number of MediaPlayers used. So instead of using oneMediaPlayerfor the bingSound, I use a new class, which internally creates an array of MediaPlayers. bingSound is an instance of this helper class. If i call bingSound.play(), the helper class initializes the firstMediaPlayer in the array and starts it. When bingSound.play() is called again, the helper class takes the secondMediaPlayerin the array and so on. With this I can fire off the same sound very quickly several times, without having to make sure that the sound has finished before it can be played once more. For all MediaPlayers created I set the onCompletionListener as you suggested with the effect you described: the system does not fill up withMediaPlayerremnants any more. With this everything works fine now. All in all pretty complex though. On 29 Okt., 09:27, blindfold [EMAIL PROTECTED] wrote: With option 2, did you try using a setOnCompletionListener() with a bingSound.stop() and bingSound.release()? This cleaning up ought to prevent your system from filling up withMediaPlayerremnants - unless of course you make so many calls to bingSound.start() per unit of time that the system has no chance (on average) to finish playing one bingSound before the next bingSound request already comes in. This approach works for me. Regards --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Playing sounds multiple times using MediaPlayer
Glad it worked! Your use of multiple MediaPlayers is an interesting approach. I current use only one, but latency is according to G1 user reports awful: 4 or 5 seconds, though I do not know yet if that is perhaps also due to the fact that Android forces me to first write synthesized sound to flash for lack of direct access to MediaPlayer's audio buffer, or due to the apparently lesser support for WAV (which I use) versus OGG according to David. In any case the emulator runs much faster for me. On Nov 2, 5:20 pm, tobias429 [EMAIL PROTECTED] wrote: Thanks for the tip! This actually does the trick. What I'm doing now is a bit more complex. I've created a helper class that multiplies the number of MediaPlayers used. So instead of using one MediaPlayer for the bingSound, I use a new class, which internally creates an array of MediaPlayers. bingSound is an instance of this helper class. If i call bingSound.play(), the helper class initializes the first MediaPlayer in the array and starts it. When bingSound.play() is called again, the helper class takes the second MediaPlayer in the array and so on. With this I can fire off the same sound very quickly several times, without having to make sure that the sound has finished before it can be played once more. For all MediaPlayers created I set the onCompletionListener as you suggested with the effect you described: the system does not fill up with MediaPlayer remnants any more. With this everything works fine now. All in all pretty complex though. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Playing sounds multiple times using MediaPlayer
Thanks for the tip! This actually does the trick. What I'm doing now is a bit more complex. I've created a helper class that multiplies the number of MediaPlayers used. So instead of using one MediaPlayer for the bingSound, I use a new class, which internally creates an array of MediaPlayers. bingSound is an instance of this helper class. If i call bingSound.play(), the helper class initializes the first MediaPlayer in the array and starts it. When bingSound.play() is called again, the helper class takes the second MediaPlayer in the array and so on. With this I can fire off the same sound very quickly several times, without having to make sure that the sound has finished before it can be played once more. For all MediaPlayers created I set the onCompletionListener as you suggested with the effect you described: the system does not fill up with MediaPlayer remnants any more. With this everything works fine now. All in all pretty complex though. On 29 Okt., 09:27, blindfold [EMAIL PROTECTED] wrote: With option 2, did you try using a setOnCompletionListener() with a bingSound.stop() and bingSound.release()? This cleaning up ought to prevent your system from filling up with MediaPlayer remnants - unless of course you make so many calls to bingSound.start() per unit of time that the system has no chance (on average) to finish playing one bingSound before the next bingSound request already comes in. This approach works for me. Regards --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Playing sounds multiple times using MediaPlayer
I've been trying to switch my soundpool based app to use mediaplayer but the mediaplayer just feels so laggy compared to the quick, reliable responsiveness of soundpool. Honestly I'm just not happy even attempting to use mediaplayer for games. It's not suited for it even one bit. On Oct 31, 6:42 pm, Dave [EMAIL PROTECTED] wrote: For one-shot sounds, don't set loop, just use the following each time you want to trigger a sound: bingSound.seekTo(0); bingSound.start(); On Oct 28, 2:01 pm, tobias429 [EMAIL PROTECTED] wrote: I am looking for a solution to play sounds multiple times in an application. In the application, as soon as a user performs operation A, the application shall respond with a *bing* sound. Performing operation B shall produce a *bong*. That means during the application life cycle, both sounds will potentially be played several dozen times. I have tried three different ways to achieve this. None of them seems to work: For all options, I have added 2 wav files, one for *bing* and one for *bong* in the application's Resource Package. Option 1: === For both wav files, I am opening a MediaPlayer via e.g. bingSound=MediaPlayer.create(context, resid). In the application, whenever a sound shall be played, I call bingSound.start(). Result: This works fine the first time. If I call bingSound.start() a second time no sound plays, and I get an exception that I called start() on a MediaPlayer instance in the wrong state. Option 2: === Whenever the application shall play a sound, I create a new MediaPlayer from scratch via e.g. bingSound=MediaPlayer.create(context, resid); bingSound.start() Result: This works fine a number of times, until appearantly the maximum number of MediaPlayers is reached, and the application simply halts. Option 3: === I create a new MediaPlayer via MediaPlayer.create(context, resid). I set looping to true. When the application shall play the sound, I call bingSound.start(). When the sound shall stop, I call bingSound.seekTo(0), followed by bingSound.pause(). Result: This works fine for looping sounds. For the classical *bing* sound that shall only be played once and does not loop, it is next to impossible to time the calls in a way that the sound is only played once. Question === Is there any possible approach to play the same, non looping sound several dozen times in an application? Thanks for any help! Tobias --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Playing sounds multiple times using MediaPlayer
I advise against using SoundPool. It is not part of the official SDK and will likely change in the future. There are also some bugs that can cause your thread to deadlock. On Oct 28, 8:42 pm, Robert Green [EMAIL PROTECTED] wrote: I used SoundPool. It's super buggy but if you're careful it does work. I hope they clean it up in a future release. Do a search for SoundPool and you'll find the message with my results in it. On Oct 28, 4:01 pm, tobias429 [EMAIL PROTECTED] wrote: I am looking for a solution to play sounds multiple times in an application. In the application, as soon as a user performs operation A, the application shall respond with a *bing* sound. Performing operation B shall produce a *bong*. That means during the application life cycle, both sounds will potentially be played several dozen times. I have tried three different ways to achieve this. None of them seems to work: For all options, I have added 2 wav files, one for *bing* and one for *bong* in the application's Resource Package. Option 1: === For both wav files, I am opening a MediaPlayer via e.g. bingSound=MediaPlayer.create(context, resid). In the application, whenever a sound shall be played, I call bingSound.start(). Result: This works fine the first time. If I call bingSound.start() a second time no sound plays, and I get an exception that I called start() on a MediaPlayer instance in the wrong state. Option 2: === Whenever the application shall play a sound, I create a new MediaPlayer from scratch via e.g. bingSound=MediaPlayer.create(context, resid); bingSound.start() Result: This works fine a number of times, until appearantly the maximum number of MediaPlayers is reached, and the application simply halts. Option 3: === I create a new MediaPlayer via MediaPlayer.create(context, resid). I set looping to true. When the application shall play the sound, I call bingSound.start(). When the sound shall stop, I call bingSound.seekTo(0), followed by bingSound.pause(). Result: This works fine for looping sounds. For the classical *bing* sound that shall only be played once and does not loop, it is next to impossible to time the calls in a way that the sound is only played once. Question === Is there any possible approach to play the same, non looping sound several dozen times in an application? Thanks for any help! Tobias --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Playing sounds multiple times using MediaPlayer
I used SoundPool. It's super buggy but if you're careful it does work. I hope they clean it up in a future release. Do a search for SoundPool and you'll find the message with my results in it. On Oct 28, 4:01 pm, tobias429 [EMAIL PROTECTED] wrote: I am looking for a solution to play sounds multiple times in an application. In the application, as soon as a user performs operation A, the application shall respond with a *bing* sound. Performing operation B shall produce a *bong*. That means during the application life cycle, both sounds will potentially be played several dozen times. I have tried three different ways to achieve this. None of them seems to work: For all options, I have added 2 wav files, one for *bing* and one for *bong* in the application's Resource Package. Option 1: === For both wav files, I am opening a MediaPlayer via e.g. bingSound=MediaPlayer.create(context, resid). In the application, whenever a sound shall be played, I call bingSound.start(). Result: This works fine the first time. If I call bingSound.start() a second time no sound plays, and I get an exception that I called start() on a MediaPlayer instance in the wrong state. Option 2: === Whenever the application shall play a sound, I create a new MediaPlayer from scratch via e.g. bingSound=MediaPlayer.create(context, resid); bingSound.start() Result: This works fine a number of times, until appearantly the maximum number of MediaPlayers is reached, and the application simply halts. Option 3: === I create a new MediaPlayer via MediaPlayer.create(context, resid). I set looping to true. When the application shall play the sound, I call bingSound.start(). When the sound shall stop, I call bingSound.seekTo(0), followed by bingSound.pause(). Result: This works fine for looping sounds. For the classical *bing* sound that shall only be played once and does not loop, it is next to impossible to time the calls in a way that the sound is only played once. Question === Is there any possible approach to play the same, non looping sound several dozen times in an application? Thanks for any help! Tobias --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---