尝试录制音频文件未保存

时间:2019-01-13 16:24:02

标签: ios swift audio avfoundation avaudiorecorder

我的代码正在尝试记录声音并将其显示在表格视图中。但是,似乎没有保存音频。我在日志文件中看到此消息。

  

2019-01-13 11:11:07.926903-0500反射声音[2902:31076] 317:ca_debug_string:inPropertyData == NULL

我不知道那是什么意思。似乎没有音频正在记录,并且当我在tableview中单击它时,它无法正常工作。我正在尝试遵循本教程https://www.youtube.com/watch?v=CrLbuzM9IDs

import UIKit;import AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDataSource, UITableViewDelegate {
    var recordingSessioin: AVAudioSession!
    var audioRecorder: AVAudioRecorder!
    var aduioPlayer : AVAudioPlayer!

    @IBOutlet var mytableVie3w : UITableView!

    var numberOfRecords = 0
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numberOfRecords
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)
        cell.textLabel?.text = String(indexPath.row + 1)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")

        do {
        aduioPlayer = try AVAudioPlayer(contentsOf: path)
            aduioPlayer.play()


        }
        catch {

        }
    }
    @IBOutlet var buttonL:UIButton!

    @IBAction func record() {

        if audioRecorder == nil{
            numberOfRecords += 1
            let fileName = getDirectory().appendingPathComponent("\(numberOfRecords).m4a")

            let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey : 12000, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey : AVAudioQuality.high.rawValue]


            do {
                audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)

                audioRecorder.delegate = self
                audioRecorder.record()
                buttonL.setTitle("Stop", for: .normal)
            }
            catch {
                displayALert(title: "Ups", message: "Recording Failed")
            }


        }

        else {
            audioRecorder.stop()
            audioRecorder = nil
            buttonL.setTitle("Start", for: .normal)

            UserDefaults.standard.set(numberOfRecords, forKey: "myN")
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        recordingSessioin = AVAudioSession.sharedInstance()

        AVAudioSession.sharedInstance().requestRecordPermission { (hasPermsionn) in
            if hasPermsionn{
                print("Yes")
            }
        }


        if let number:Int = UserDefaults.standard.object(forKey: "myN") as? Int
        {
            numberOfRecords = number
        }
    }

    func getDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = paths[0]
        return documentDirectory

    }

    func displayALert(title : String, message: String){
        let alert = UIAlertController(title: title, message: "dismiss", preferredStyle:     .alert)
        alert.addAction(UIAlertAction(title: "diss", style: .default, handler: nil))

        present(alert, animated: true, completion: nil)
    }
}

0 个答案:

没有答案