Swift-示例-如何在使用AVAudioRecorder录制前后播放蜂鸣音?

Swift-示例-如何在使用AVAudioRecorder录制前后播放蜂鸣音?,swift,session,audio,record,playback,Swift,Session,Audio,Record,Playback,我想使用Swift在iOS中实现类似Whatsapp的录制按钮,当用户按下按钮时,一声蜂鸣音表示开始,然后录制开始,当用户松开按钮时,录制停止,另一声蜂鸣音表示录制完成 我尝试使用以下代码实现此功能: func startRecording(sender: UIDataButton?, callData: NSDictionary?) -> Bool { do { if (self.audioRecorder == nil) { Audio

我想使用Swift在iOS中实现类似Whatsapp的录制按钮,当用户按下按钮时,一声蜂鸣音表示开始,然后录制开始,当用户松开按钮时,录制停止,另一声蜂鸣音表示录制完成

我尝试使用以下代码实现此功能:

func startRecording(sender: UIDataButton?, callData: NSDictionary?) -> 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(), { () -> 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?) -> 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(), { () -> Void in
                sender!.setTitle("Push-to-Talk", forState: .Normal)
            })
        }
    }
但问题是JBL_Begin alert声音永远不会播放

另外,当我试着在录制后播放录制的音频时,音频的音量非常低。这是我的密码:

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)")
        }
    }
}

知道出现此问题的原因吗?

这是因为当您尝试播放系统声音时,AVAudioSession处于活动状态。您需要完整地播放系统声音,然后将其设置为true并开始录制

AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1110)) {
    //Recording method here
}
在完成块内,您将运行记录方法,包括:

try self.audioSession.setActive(true)
然后在播放结束系统声音之前,请确保将其设置回false

try self.audioSession.setActive(false)

你找到解决办法了吗?@Nij我可能有点晚了,但我在下面回答。