1
class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recodinginProgress: UILabel!
@IBOutlet weak var stopButton: UIButton!

var audioPlayer: AVAudioPlayer!
var recordAudio: RecordedAudio!
var audioRecorder:AVAudioRecorder

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
    //hide the stop button
    stopButton.hidden = true
    recordButton.enabled = true
}

@IBAction func recordAudio(sender: UIButton) {
    recordButton.enabled = false
    stopButton.hidden = false
    recodinginProgress.hidden = false
    //TODO: Record the user"s voice
    let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
                                                .UserDomainMask, true)[0] as String
    let currentDateTime = NSDate()
    let formatter = NSDateFormatter()
    formatter.dateFormat = "ddMMyyyy-HHmmss"
    let recordingName = formatter.stringFromDate(currentDateTime)+".wav"
    let pathArray = [dirPath, recordingName]
    let filePath = NSURL.fileURLWithPathComponents(pathArray)
    println(filePath)

    var session = AVAudioSession.sharedInstance()
    session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
    audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)
    audioRecorder.delegate = self
    audioRecorder.meteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()

}

I don't know what I am doing wrong. My error is coming on my class.

It says

class RecordSoundsViewController has no initializers on RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
Alan Isaac
  • 11
  • 2

1 Answers1

2

The error message is a bit bad from the compiler. The reason you see the error is because you have a property which does not have a default value.

In Swift all values needs to have a default value unless it's an optional. In your case it's this property: var audioRecorder:AVAudioRecorder

In your case I would make this property an optional: var audioRecorder:AVAudioRecorder? and make sure to check for nil when using. Or to make it an implicitly unwrapped optional (if you know there's always gonna be a value): var audioRecorder:AVAudioRecorder!

Markus Persson
  • 1,103
  • 9
  • 21