CBAudioPlayer

Posted by

My first CocoaPod – had this little snippet laying around for a while now and thought it might be a nice little first contribution to the CocoaPod community. 

A simple wrapper for AVAudioPlayer() that gives you a completion handler which fires when the audio has finished playing.

let audioPlayer = CBAudioPlayer()

// ...

guard let audioFile = Bundle.main.url(forResource: "BrownFox", withExtension: "m4a") else {
    return
}
        
audioPlayer.playAudioFile(url: audioFile) { (success, status) in
            
    if success {
        self.showMessage(message: "Audio played from URL()")
    }
    else {
        self.showMessage(message: "Audio failed from URL()")
    }
            
    print("Is playing: " + (self.audioPlayer.isPlaying ? "YES" : "NO"))
            
}
        
print("Is playing: " + (audioPlayer.isPlaying ? "YES" : "NO"))

Above is a typical example of how you might use this pod, comments & pull requests welcome as always.

For further information on AVAudioPlayer see the following URL https://developer.apple.com/documentation/avfoundation/avaudioplayer

developer.apple.com

The CocoaPod is available here and direct source on BitBucket here

Enjoy

C. 🥃