异步调用录音比自动播放比删除音频
Async calling for recording than autoplay than delete the audio
按下按钮我应该录制音频 4 秒并自动播放并循环播放 2 次。另一个按下按钮我应该做同样的事情但在此之前我应该删除以前的记录。
这是我的代码:
@IBAction func loopButton(_ sender: Any) {
if audioRecorder?.isRecording == false {
playButton.isEnabled = false
audioRecorder?.record(forDuration: 4.0)
audioRecorder?.stop()
audioPlayer?.stop()
do {
try audioPlayer = AVAudioPlayer(contentsOf: (audioRecorder?.url)!)
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = 2
audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
} else {
audioRecorder?.deleteRecording()
audioRecorder?.record(forDuration: 4.0)
audioRecorder?.stop()
audioPlayer?.stop()
do {
try audioPlayer = AVAudioPlayer(contentsOf: (audioRecorder?.url)!)
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = 2
audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}
我应该异步执行此操作,有人可以帮助我吗?
谢谢
这份工作是我的,希望对您有所帮助
@IBAction func loopButton(_ sender: Any) {
DispatchQueue.main.async {
if self.audioRecorder?.isRecording == false {
self.audioRecorder?.record(forDuration: 4.0)
self.playButton.isEnabled = false
} else {
self.audioRecorder?.deleteRecording()
self.audioRecorder?.record(forDuration: 4.0)
self.playButton.isEnabled = false
}
}
DispatchQueue.global().asyncAfter(deadline: .now() + 4.0) {
do {
try self.audioPlayer = AVAudioPlayer(contentsOf: (self.audioRecorder?.url)!)
self.audioPlayer!.delegate = self
self.audioPlayer!.prepareToPlay()
self.audioPlayer!.numberOfLoops = 2
self.audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}
按下按钮我应该录制音频 4 秒并自动播放并循环播放 2 次。另一个按下按钮我应该做同样的事情但在此之前我应该删除以前的记录。
这是我的代码:
@IBAction func loopButton(_ sender: Any) {
if audioRecorder?.isRecording == false {
playButton.isEnabled = false
audioRecorder?.record(forDuration: 4.0)
audioRecorder?.stop()
audioPlayer?.stop()
do {
try audioPlayer = AVAudioPlayer(contentsOf: (audioRecorder?.url)!)
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = 2
audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
} else {
audioRecorder?.deleteRecording()
audioRecorder?.record(forDuration: 4.0)
audioRecorder?.stop()
audioPlayer?.stop()
do {
try audioPlayer = AVAudioPlayer(contentsOf: (audioRecorder?.url)!)
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = 2
audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}
我应该异步执行此操作,有人可以帮助我吗?
谢谢
这份工作是我的,希望对您有所帮助
@IBAction func loopButton(_ sender: Any) {
DispatchQueue.main.async {
if self.audioRecorder?.isRecording == false {
self.audioRecorder?.record(forDuration: 4.0)
self.playButton.isEnabled = false
} else {
self.audioRecorder?.deleteRecording()
self.audioRecorder?.record(forDuration: 4.0)
self.playButton.isEnabled = false
}
}
DispatchQueue.global().asyncAfter(deadline: .now() + 4.0) {
do {
try self.audioPlayer = AVAudioPlayer(contentsOf: (self.audioRecorder?.url)!)
self.audioPlayer!.delegate = self
self.audioPlayer!.prepareToPlay()
self.audioPlayer!.numberOfLoops = 2
self.audioPlayer!.play()
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}