问题描述
import UIKit
import AVFoundation
class BeginRecording: UIViewController,AVAudioRecorderDelegate,AVAudioPlayerDelegate {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var playButton: UIButton!
var audioPlayer : AVAudioPlayer?
var audioRecorder : AVAudioRecorder?
var recordingSession: AVAudioSession?
override func viewDidLoad() {
super.viewDidLoad()
playButton.isEnabled = false
stopButton.isEnabled = false
let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for: .documentDirectory,in: .userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("sound.caf")
let recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,AVEncoderBitRateKey:
16,AVNumberOfChannelsKey: 2,AVSampleRateKey: 44100.0] as [String : Any]
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord)
try recordingSession?.setActive(true)
recordingSession?.requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
self.prepareForRecord(soundFileURL: soundFileURL,recordSettings: recordSettings)
print("Began to prepare for recording")
} else {
print("Failed to record")
self.dismiss(animated: true,completion: nil)
}
}
}
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
}
func prepareForRecord(soundFileURL: URL,recordSettings: [String : Any]) {
do {
try audioRecorder = AVAudioRecorder(url: soundFileURL,settings: recordSettings as [String : AnyObject])
audioRecorder?.prepareToRecord()
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
}
@IBAction func recordAudio(_ sender: AnyObject) {
if audioRecorder?.isRecording == false {
playButton.isEnabled = false
stopButton.isEnabled = true
audioRecorder?.record()
print("Now recording")
} else {
audioRecorder?.prepareToRecord()
audioRecorder?.record()
print("Already recording")
}
}
@IBAction func stopAudio(_ sender: AnyObject) {
stopButton.isEnabled = false
playButton.isEnabled = true
recordButton.isEnabled = true
if audioRecorder?.isRecording == true {
audioRecorder?.stop()
print("Stopped Recording")
} else {
audioPlayer?.stop()
print("Stopped audio")
}
}
@IBAction func playAudio(_ sender: AnyObject) {
if audioRecorder?.isRecording == false {
stopButton.isEnabled = true
recordButton.isEnabled = false
do {
try audioPlayer = AVAudioPlayer(contentsOf:
(audioRecorder?.url)!)
audioPlayer!.delegate = self
audioPlayer!.prepareToPlay()
audioPlayer!.volume = 1.0
audioPlayer!.play()
print("Began playing back audio")
} catch let error as NSError {
print("audioPlayer error: \(error.localizedDescription)")
}
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,successfully flag: Bool) {
recordButton.isEnabled = true
stopButton.isEnabled = false
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer,error: Error?) {
print("Audio Play Decode Error")
}
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder,successfully flag: Bool) {
}
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder,error: Error?) {
print("Audio Record Encode Error")
}
}
这是一个在我的应用程序上运行 ViewController 的类。但是,每次我去运行它时,控制台都会打印出“已经录制”,就像录制已打开一样。没有任何正在开始录制的事情,如果它正在录制,它不应该让我点击开始另一个录制的 IBAction 按钮,但它仍然允许它。播放按钮始终处于禁用状态,停止按钮也是如此。我也没有收到任何音频。另外,我不知道这是否是一个问题,但它不会将“开始准备录制”打印到控制台。我不知道那是不是因为我已经接受了隐私条件,但是在 Dispatch Queue 中无法访问该 if 语句。有谁知道解决这些问题的方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)