Implementing the iOS SoundHandler using the AVAudioPlayer framework
The AVAudioPlayer
class is the framework we will be using to play and control our audio streams in iOS, so let's begin by adding a new folder called Sound
to the iOS project. We then want to create a new file called SoundHandler.cs
that will inherit the ISoundHandler
interface:
public class SoundHandler : ISoundHandler { }
Now let's create a private AVAudioPlayer
object and add our public IsPlaying
, which will hold the playing status of the audio player:
private AVAudioPlayer _audioPlayer; public bool IsPlaying { get; set; }
Then we add in the functions of the interface. In each function, we will be using the audio player object to do all our audio processing:
public void Load() { _audioPlayer = AVAudioPlayer.FromUrl(NSUrl.FromFilename("Moby - The Only Thing.mp3")); } public void PlayPause() { if (_audioPlayer != null) { ...