Audio Recording and Playing in Swift 3.0 - PART 2

This is the Second part of this tutorial, in the first part we are study about audio recording, Now here we are only discussing, How to play audio which is already recorded.



The main class of Audio player AVAudioPlayer. You are study recording the audio before the playing. You have to study the previous tutorial to Recording audio file.

Audio Recording and Playing in Swift 3.0.

Step 1 : Import the AVFoundation framework in your file.
import AVFoundation
Step 2 :  Adding audioRecorded Delegate in your swift file.
class ViewController: UIViewController , AVAudioRecorderDelegate , AVAudioPlayerDelegate {
Step 3:  Adding the property to play audio player.
var audioPlayer : AVAudioPlayer!

Step 4 :  You have to audio recording before the click on the button of audio playing. See here to recording audio.
@IBAction func doPlay(_ sender: AnyObject) {
    if !audioRecorder.isRecording {
        self.audioPlayer = try! AVAudioPlayer(contentsOf: audioRecorder.url)
        self.audioPlayer.prepareToPlay()
        self.audioPlayer.delegate = self
        self.audioPlayer.play()
    }
}

Step 5 :  When you play the audio then its AVAudioPlayerDelegate method calling.
AVAudioPlayerDelegate
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    print(flag)
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?){
    print(error.debugDescription)
}
internal func audioPlayerBeginInterruption(_ player: AVAudioPlayer){
    print(player.debugDescription)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Note : You recorded file doesn't play the connect microphone to device.


Thanks.
Audio Recording and Playing in Swift 3.0 - PART 2 Audio Recording and Playing in Swift 3.0 - PART 2 Reviewed by Kirit Modi on 01:00:00 Rating: 5

No comments:

Powered by Blogger.