Open BioTurboNick opened 5 years ago
This is great - I'll do some additional research, we could potentially swap out MediaPlayer or make this an option.
I did notice something weird recently when using this technique, just to warn you: if there's background audio playing, there's a delay and the sounds may not play through headphones. I may have misconfigured something though.
Update: the issues I described above were due to setting the Android.Media.AudioFlags.AudibilityEnforced
flag. Removing it allowed headphone playback and did not interfere with playback of background audio. Original post's code updated.
In this vein, I wanted to present my variant that acts like Android's SoundPool. iOS has no issues with latency, but Android does. This standardizes the usage of the two.
I've also adapted the multi targeting project approach used by InAppBilling for simpler code.
A lot of capabilities stripped down, some could be added back.
A further note: I have still needed to create several sound pools and rotate them in a Queue if I want to be sure two of the same sound can play simultaneously. (Though I could also have loaded the same sound twice and addressed it with different identifiers.)
I discovered this was all necessary when I blew out Android's thread engine trying to spin up a hundred SoundPool objects simultaneously with one sound each. 😬
It's possible that people who use your SoundPlayer will prefer the SoundPool approach when using sound effects vs. playing music or other long-running sounds.
I don't think it's worth me maintaining a whole separate project.
A note: There's an off-by-one error in iOS implementation that you'll need to fix for it to work. Increment returned indexes in it by 1 and decrement passed indexes by 1. Also add a using statement to each of the Load methods.
I had a severe issue with latency and bunched up sounds when using this plugin on Android for sound effects.
I found SoundPool, and simply by changing the backing Android class to SoundPool instead of MediaPlayer, I was able to eliminate this latency.
I'm not submitting a pull request because my solution is hacky. However, this is the construction, if you wish to use it as a starting point;
Loading methods are similar, but assign priority and save a soundId b/c meant for multiple sounds. Play method requires volume and looping set and all playback methods require the soundId. No Completed event available.
Probably not the most efficient since it is meant to load and play multiple small files, but it worked with just one file per pool.