1 Reply
      Latest reply on Mar 24, 2018 2:07 PM by DocRace
      ferasOS Level 1 Level 1 (0 points)

        Hi All,


        I am trying to prototype code in playgrounds before putting it into my app. Can we record Audio using swift playgrounds and how?


        Many thanks,


        Feras A.


        current attempt:


        import UIKit

        import AVFoundation


        var soundRecorder: AVAudioRecorder!

        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

        let OutputFilePath = documentsDirectory.appendingPathComponent("out.wav")


        //set the settings for recorder

        var recordSettings = [

            AVFormatIDKey: kAudioFormatAppleLossless,

            AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,

            AVEncoderBitRateKey : 320000,

            AVNumberOfChannelsKey: 2,

            AVSampleRateKey : 44100.0

        ] as [String : Any]


        try soundRecorder = AVAudioRecorder(url: OutputFilePath, settings: recordSettings)

        soundRecorder.record(forDuration: 10)

        let audioFile = try AVAudioFile(forReading: OutputFilePath)