I need to play sound effects in my game such as jump, failed, shot etc.
So for this purpose I have selected SoundPool class. But there were some times when it lagged.
So I wonder what is correct way to use this class. I have tried to use play method in new thread, it seems work better but I don't know if I am correct.
Please advice how to use object of this class correctly for better performance.
Best way to implement Sound / Music into your game is by using Service component. Read this answer here for an example.
Related
I need to embed simple inline audio playback into a list view, where the user would just click play/pause/stop on the items one by one, in the foreground
The playback back would stop when leaving the screen (activity or fragment).
So the use case is similar to those usual chat apps.
Ideally, I was looking for a simple UI widget I could embed into a list view and pass a mp3/wav/ogg url (local files for a start).
Trying to research for such a thing I found people implementing either an ultra simplistic player with no ui at all or a fully fledged bound Service (which I tried and it works, but it seems like an overkill for a simple foreground playback). When simply embedding a MediaPlayer + MediaController into a fragment, the android monitor was complaining -I think- about doing too much on the UI thread.
So, is there such a player widget to minimise boilerplate code?
Would ExoPlayer be a good choice? ( I see it's powerful but may be even more of an overkill - unless it has some handy utility class, which I missed so far)
What would be the simplest, quickest, safest, most elegant solution?
I'd be grateful for any pointers or enlightenment.
In the meantime I found some potential candidates as audio player widgets:
Uncodin: looks good but not properly packaged, as a gradle dependency https://github.com/Uncodin/Android-Common/blob/master/UncodinCommon-standalone/src/in/uncod/android/media/widget/AudioPlayerView.java
https://github.com/Cleveroad/MusicBobber
and more on android-arsenal.com
I've yet to try these but wouldn't mind hearing suggestions about concrete solutions or how to implement or where to look.
does someone know what is the best way to implement the audio loop-back functionality in an android app?
I need to start recording sound and play that recorded sound through the headphones at the same time.
I was thinking of storing the recorded data in an buffer, and passing that buffer to mediaPlayer object within the while loop. What do you guys think, would it be feasible or is there a better approach to this(some lib,etc.)?
I would suggest you take a look at Exoplayer since it provides you ways to implement your custom data source by implementing DataSource class. So you can implement the DataSource to use a stream to play the audio. The documentation is a little vague on how to override it but if you are willing to invest a bit of time to look into exoplayer code you will easily figure it out. Though not exactly your scenarion here is one of the POCs to show how custom source can be implemented CustomDataSource
I am currently trying to create an android app that plays a different sequence of sounds based on user input. Is there a simple way to play a sequence of sounds I have looked at mediaplayer and soundpool but it does not look like they where designed for sequential.
Thank you RandyFreak that is probably the better way. But I actually Ended up doing it a little differently.
I created a list of fileDescriptors pointing to sounds and added an oncomplete listener to mediaplayer. And every time the media player completed it's current sound it would play the first sound in the list and remove it from the list.
I came across this issue a few months ago.
How I overcame this was I created a different mediaplayer for each sound and played them in the order that I needed.
That would be the simplest method.
I hope this helps
RF
For my application I need to play multiple tracks simultaneously and some kinf of listener to know when a track is finished.
SoundPool seems to be the obvious choice, but unfortunately I could't find a solution to get notified when a track ends.
I also thought about using multiple instances of Android's MediaPlayer, but I'm afraid of getting performance issues.
Are there any other options?
If you use MediaPlayer, you can use OnCompletionListener. It also has no problem of playing multiple tracks at the same time. Like gian1200 says. Try it, perhaps the performance is not really an issue.
I apologize up-front if this is a really obvious question since I'm pretty sure I'm over-thinking it.
Anyway, I'm building a music player in Android which basically streams playlists from 8Tracks and, as my understanding is, the best-practice here would be using a foreground service. I've already built everything and it's completely functional sans service, however, I'm a little confused as to how to implement the service.
As is, I have my Player class, which controls the MediaPlayer, extending Service but I'm not quite sure where to progress from there. I've tried binding it to my Engine class which reconciles all the background work with the UI, however I keep getting a ServiceConnectionLeaked exception, and I'm fairly certain this is simply the wrong approach.
I would appreciate any help at all; really a nod in the right direction is all I need. Thanks for your time!
I think that you can directly control MediaPlayer without additional Player class. As I know MediaPlayer is already a service so you can use start and stop methods to play something.