1

I have started learning iOS app development. While making the first app I just have encountered a problem. I also tried to look out for a solution given for already asked questions but could found any error. Can anybody please tell me whats the error in the code. I have used 2 buttons and 1 label and joined it with 3 IBOutlets and 2 buttons with 2 IBActions. Guide me, please... I have found same question and tried almost all things that I found there. After that I've asked my question...

class RecordSoundsViewController: UIViewController {
  var audioRecorder: AVAudioRecorder!


  @IBOutlet weak var recordingLabel: UILabel!
  @IBOutlet weak var recordButton: UIButton!
  @IBOutlet weak var stopRecordingButton: UIButton!

  override func viewDidLoad() {
    super.viewDidLoad()
    stopRecordingButton.isEnabled = false
  }

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
  }


  @IBAction func recordAudio(_ sender: AnyObject) {
    recordingLabel.text = "Recording in progress"
    stopRecordingButton.isEnabled = true
    recordButton.isEnabled = false

    let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let recordingName = "recordedVoice.wav"
    let pathArray = [dirPath, recordingName]
    let filePath = URL(string: pathArray.joined(separator: "/"))


    let session = AVAudioSession.sharedInstance()
    try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)

    try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
    audioRecorder.isMeteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()

  }

  @IBAction func stopRecording(_ sender: UIButton) {
    recordButton.isEnabled = true
    stopRecordingButton.isEnabled = false
    recordingLabel.text = "Tap to Record"
    audioRecorder.stop()

    let audioSession = AVAudioSession.sharedInstance()
    try! audioSession.setActive(false)
  }
}
Hamza
  • 11
  • 4
  • 3
    It's most likely a dead connection in Interface Builder. Press ⇧⌘F, type `tapToRecord` and check the search result(s) related to Interface Builder. Remove the dead connection. – vadian Jun 12 '17 at 04:21

0 Answers0