Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Playing sounds effiecently.

 
zaid abdul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like input on how to proceed with my code. i have multiple short sound clips (over 100) that will be played based on what the user clicked.

every time an event happens, i check if media player is playing, and stop it if it is. and then used a switch to find the right sound to play. i dont have much experiacne with audio, and i would like some feedback on how my code is ( performance,unforeseen errors, bad habit, etc..), here is my code:

i plan to have up to a maximum of 200 different cases.

thanks for reading, any feedback is appreciated.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be my preference to re-use the MediaPlayer instead of creating a new one all the time as a way of conserving memory. But I am not sure that is possible using resourceIds as the source of the sound. Depending on what your app does this might not be a problem (how often does the sound change, for example).

If you have lots of sounds you want to use, you might consider using SoundPool. The SoundPool keeps track of the number of sounds allowed to play at once, and will handle stopping old ones when a new one is played. A different API but a good one if you are worried about managing the sound yourself.

A downside is that you get Yet-Another-ID to track that identifies the sound in the pool, so you would need to map the soundIds the pool gives you to the resourceIds that your application context knows about.
 
zaid abdul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sound changes based on user interaction, so unpredictable and i am going to assume it changes every time for safety.Soundpool did not seem that helpful for my situation. i have tested this same code with one of the longest sounds i expect to play, and i had no issues. the only thing that changed is that i don't just stop the media player, i also release it everytime so it does not hog resources.




i only tested this on an emulator though, it looks like this is the code i will be using. any feedback?
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used SoundPool before for audible UI notifications and haven't found issues. Why do you think it would not be suitable or your use?

 
zaid abdul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is a little confusing to me, i don't know how i would apply it to my code. is there any issue in creating a new mediaplayer for each sound event? the media player is released right afterwords, so it should not hog memory. and i tested it on a device with the longest possible sound i plan to play, no issues.
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - I just clipped some working code from a project - it might be a bit unclear without seeing the rest of the application.

Here is an example using the resources that you have defined in your example code:

 
zaid abdul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, that seems like a better option then what i was doing.

i do have one last question, as i am not familiar with soundpool. is there anything i need to add to the the android life cycle methods like i did with mediaplayer? here are the lifecycle methods i had to use for media player to ensure no errors happen when navigating in/out of my activity.



and last but not least, thank you for the help.

 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are better with your idea with MediaPlayer. I have never tried adding long-playing sound clips to the SoundPool's audio cache before, but when I tried just now, the OS complained about running-out of heap space. When I have used it, it was for very short beep-like notifications. If you plan on making something like a sound board, I don't think it will be suitable. The trade-off might be some extra latency from when you press the button until when the sound plays, but it will probably be so small that it will not really matter.

 
zaid abdul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point on the audio cache, i assume loading every single sound into the soundpool would have caused some major errors down the road. especially if its over 100+ sounds.

Thanks for the new MediaPayer code, much more "proper" then my code.

Thanks for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic