Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Playing sounds effiecently.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 4170
555
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 4170
555
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 4170
555
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
eat bricks! HA! And here's another one! And a tiny ad!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic