AVAudioRecorder 和 AVAudioPlayer 不适用于 IBAction 按钮点击

问题描述

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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...