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) 
            { 
                if (IsPlaying) 
                { 
                    _audioPlayer.Stop(); 
                } 
                else 
                { 
                    _audioPlayer.Play(); 
                } 
 
                IsPlaying = !IsPlaying; 
            } 
        } 

The first function will load the file from the Resources folder. In this example, we are going to be loading in a Moby song (personally one of my favorites).

Note

You can add in any audio file, provided the name matches the filename being loaded via the NSURL object. If you want to use the same file as this one, visit the GitHub link stated previously.

The second function will control starting and stopping the audio. If we click the play button first, it will play and set the status of IsPlaying to true. Then if we click the play button again, it will stop the audio and set the IsPlaying to false.

Now for the rest of the implementation:

public void Stop() 
        { 
            if (_audioPlayer != null) 
            { 
                _audioPlayer.Stop(); 
            } 
        } 
 
        public double Duration() 
        { 
            if (_audioPlayer != null) 
            { 
                return _audioPlayer.Duration; 
            } 
 
            return 0; 
        } 
 
        public void SetPosition(double value) 
        { 
            if (_audioPlayer != null) 
            { 
                _audioPlayer.CurrentTime = value; 
            } 
        } 
 
        public double CurrentPosition() 
        { 
            if (_audioPlayer != null) 
            { 
                return _audioPlayer.CurrentTime; 
            } 
 
            return 0; 
        } 
 
        public void Forward() 
        { 
            if (_audioPlayer != null) 
            { 
                IsPlaying = false; 
 
                _audioPlayer.Stop(); 
                _audioPlayer.CurrentTime = audioPlayer.Duration; 
            } 
        } 
 
        public void Rewind() 
        { 
            if (_audioPlayer != null) 
            { 
                IsPlaying = false; 
 
                _audioPlayer.Stop(); 
                _audioPlayer.CurrentTime = 0; 
            } 
        } 

All of this is straightforward: our Stop function will stop the audio. Our Rewind function will stop the audio and set the current time to 0 (meaning the beginning of the audio stream). Our Forward function will stop the audio and move the current time to the end of the stream. The last two functions will set the current position of the audio stream to the double value passed in. This will be used with our progress slider; when the slider position changes, the value will be passed into this function to update the position of the audio stream. Finally, the last function will retrieve the current time value so we can update our user interface with this detail.

Great! Now that we have our sound handler implemented for iOS, we want to register this through the IoC container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.12