title: Swift - Sample - How to play beep sound before and after recording using AVAudioRecorder?
body: <p>I want to implement similar record button like Whatsapp in iOS using Swift, where when user holds the button down, a beep sound indicates start, after which recording starts and when user releases the button, recording stops and another beep sound indicates recording finished. </p>
  
  <p>I tried implementing this feature using the following code:</p>
  
  <pre><code>func startRecording(sender: UIDataButton?, callData: NSDictionary?) -&gt; Bool {
      do {
          if (self.audioRecorder == nil) {
  
              AudioServicesPlayAlertSound(1110) // JBL_Begin
  
              self.audioSession = AVAudioSession.sharedInstance()
              try self.audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .MixWithOthers)
              try self.audioSession.setMode(AVAudioSessionModeVoiceChat)
              try self.audioSession.setActive(true)
  
              if (sender != nil) {
                  dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                      sender!.setTitle("Recording...", forState: .Normal)
                  })
              }
  
              try self.audioRecorder = AVDataAudioRecorder(URL: self.fileURL("\(CurrentTimestamp)_aud.mp4")!,
                  settings: self.recordSettings)
  
              if (sender != nil) {
                  self.audioRecorder.setRecorderSender(sender!)
              }
              if (callData != nil) {
                  self.audioRecorder.setRecorderData(callData!)
              }
  
              self.audioRecorder.delegate = self
  
              if (self.audioRecorder.prepareToRecord()) {
                  if (self.audioRecorder.record()) {
                      NSLog("audioRecorder started recording")
                      return true
                  } else {
                      self.audioRecorder = nil
                      NSLog("audioRecorder not started")
                      return false
                  }
              } else {
                  NSLog("audioRecorder failed to prepare")
                  return false
              }
  
          }
      } catch let error as NSError {
          print("Error \(error.debugDescription)")
          if (self.audioRecorder != nil) {
              self.audioRecorder.stop()
              self.audioRecorder = nil
          }
          return false
      }
  
      return false
  }
  
  func finishRecording(sender: UIDataButton?) -&gt; AVDataAudioRecorder? {
  
      var recorder: AVDataAudioRecorder? = nil
  
      if self.audioRecorder != nil {
  
          self.audioRecorder.stop()
          NSLog("audioRecorder stopped recording")
          recorder = self.audioRecorder
  
          AudioServicesPlayAlertSound(1111) // JBL_End
  
          self.audioRecorder = nil
  
          do {
              try self.audioSession.setActive(false)
          } catch let error as NSError {
              print("Error - AudioSession setActive False failed -  \(error.debugDescription)")
          }
  
  
          if (sender != nil) {
              dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                  sender!.setTitle("Push-to-Talk", forState: .Normal)
              })
          }
      }
  </code></pre>
  
  <p>But the problem is that the JBL_Begin alert sound never plays. </p>
  
  <p>Also, when I try to playback the recorded audio after recording, the volume of the audio is very low. Here is my code:</p>
  
  <pre><code>func pressAudioControl(sender: UIButton!) {
      if audioPlayer.playing {
          audioPlayer.pause()
          self.imageView.image = UIImage(named: "MessagePlay")
      } else {
          do {
              self.audioSession = AVAudioSession.sharedInstance()
              try self.audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .DefaultToSpeaker)
              try self.audioSession.setActive(true)
              self.audioPlayer.volume = 1.0
              audioPlayer.play()
              self.imageView.image = UIImage(named: "MessagePause")
  
          } catch let error as NSError {
              print("audioPlayer error \(error.debugDescription)")
          }
      }
  }
  </code></pre>
  
  <p>Any idea why this problem occurs?</p>