[英]Swift - Sample - How to play beep sound before and after recording using AVAudioRecorder?
我想在 iOS 中使用 Swift 實現類似 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 警報聲從不播放。
此外,當我在錄制后嘗試播放錄制的音頻時,音頻的音量非常低。 這是我的代碼:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.