Reputation: 221
I am using this implementation of an Audio Player in Xamarin
https://www.codeproject.com/Articles/1088094/Playing-audio-mp-File-in-Xamarin-Forms
I have a button in my code that, when pressed, plays a certain, short (1-2 seconds) tone.
The player in the link works well, but for some reason if I repeatedly press my button like 5 or 6 times the audio player doesnt work anymore. Even if I leave the page and come back the audio player still doesn't work. What could be causing this? I'm fearing that it might be a device security thing on Android, because why else would it play the first few times?
Here is my code for clicking the button
void PlayChordClicked(object sender, EventArgs e)
{
DependencyService.Get<IAudio>().PlayAudioFile(myMp3);
}
Everything else is exactly as copied from the tutorial. Thanks!
Upvotes: 2
Views: 1379
Reputation: 221
With help from Gusman in the comments, I figured out that I was most likely crashing the app by not disposing of the MediaPlayer after using it.
I added these few lines in the Android "PlayAudioFile" method and it did the trick
player.Completion += delegate
{
player.Release();
player.Dispose();
};
Upvotes: 4