Skip to content

Instantly share code, notes, and snippets.

@ufukhawk
Created February 19, 2019 09:10
Show Gist options
  • Save ufukhawk/0cb023790c7b673ef40dec195ae45c34 to your computer and use it in GitHub Desktop.
Save ufukhawk/0cb023790c7b673ef40dec195ae45c34 to your computer and use it in GitHub Desktop.
public class AudioPlayerService : IAudioPlayerService
{
private AVAudioPlayer _audioPlayer = null;
public Action OnFinishedPlaying { get; set; }
public AudioPlayerService()
{
}
public void Play(string pathToAudioFile)
{
if (_audioPlayer != null)
{
_audioPlayer.FinishedPlaying -= Player_FinishedPlaying;
_audioPlayer.Stop();
}
string localUrl = pathToAudioFile;
_audioPlayer = AVAudioPlayer.FromUrl(NSUrl.FromFilename(localUrl));
_audioPlayer.FinishedPlaying += Player_FinishedPlaying;
_audioPlayer.Play();
}
private void Player_FinishedPlaying(object sender, AVStatusEventArgs e)
{
OnFinishedPlaying?.Invoke();
}
public void Pause()
{
_audioPlayer?.Pause();
}
public void Play()
{
_audioPlayer?.Play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment