4 Replies
      Latest reply: Feb 16, 2017 3:10 PM by p.knausst RSS
      p.knausst Level 1 Level 1 (0 points)

        Hello,

         

        I am trying to record something, but I get an error in the setup with my AVAudioRecorder:

         

        Cannot invoke initializer for type 'AVAudioRecorder' with an argument list of type '(url: NSURL, settings: [String : Any], error: inout NSError?)'

         

        I'm not sure whats wrong here. Heres my code:

         

        var soundRecorder = AVAudioRecorder()
            var soundPlayer = AVAudioPlayer()
           
            var fileName = "audioFile.m4a"
           
            override func viewDidLoad() {
                super.viewDidLoad()
                /
            }
            override func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
                /
            }
           
            func setupRecorder() {
                var recordSettings = [ AVFormatIDKey : kAudioFormatAppleLossless,
                                       AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                                       AVEncoderBitRateKey: 320000,
                                       AVNumberOfChannelsKey : 2,
                                       AVSampleRateKey : 44100.0 ] as [String : Any]
               
                var error : NSError?
               
                soundRecorder = AVAudioRecorder(url: getFileURL(), settings: recordSettings, error: &error)
            }
           
            func getCacheDirectory() -> String{
                let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
                return paths[0]
            }
            func getFileURL() -> NSURL{
                let path = getCacheDirectory().appending(fileName)
                let filePath = NSURL(fileURLWithPath: path)
                return filePath
            }
        

         

        THANKS FOR HELPING!    

        • Re: How do I setup an AVAudioRecorder?
          eskimo Apple Staff Apple Staff (6,075 points)

          I am trying to record something, but I get an error in the setup with my AVAudioRecorder:

          What version of Xcode are you using?  And, if it’s an Xcode 8 flavour, what version of Swift?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: How do I setup an AVAudioRecorder?
              p.knausst Level 1 Level 1 (0 points)

              I'm using Xcode Version 8.2.1 and Swift 3.

                • Re: How do I setup an AVAudioRecorder?
                  eskimo Apple Staff Apple Staff (6,075 points)

                  I'm using Xcode Version 8.2.1 and Swift 3.

                  OK.  In that case it seems like you’ve not done your Swift 3 conversion properly.  Specifically, on line 24 you’re creating an AVAudioRecorder as if you were programming in Swift 2.  Swift 3 imports this AVAudioRecorder initialiser as:

                  init(url: URL, settings: [String : Any]) throws
                  

                  which means you need to:

                  • Call it with a Swift 3 URL value, rather than the NSURL returned by getFileURL()

                  • Handle any potential error using Swift’s error handling mechanism

                  The best way to fix the first point is to rewrite lines 27 through 35 to use URL.  Here’s how you’d do the equivalent in Swift 3:

                  let fileName = "audioFile.m4a"
                  let docDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                  let fileURL = docDirURL.appendingPathComponent(fileName)
                  

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: How do I setup an AVAudioRecorder?
                      p.knausst Level 1 Level 1 (0 points)

                      Thank you! It works!

                       

                      Here my working code for those who have the same problem:

                       

                      let recordSettings = [ AVFormatIDKey : kAudioFormatAppleLossless,
                                                     AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
                                                     AVEncoderBitRateKey: 320000,
                                                     AVNumberOfChannelsKey : 2,
                                                     AVSampleRateKey : 44100.0 ] as [String : Any]
                             
                      let docDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
                      let fileURL = docDirURL.appendingPathComponent(fileName)
                             
                      soundRecorder = try! AVAudioRecorder.init(url: fileURL, settings: recordSettings)