Mac OS x app录音生成文件。但播放该文件时声音不会到来

时间:2017-08-17 11:21:53

标签: swift macos audio-recording avaudiorecorder

我正在创建mac os x app。使用swift 3.2我能够录制音频甚至创建音频文件,但在播放该文件时,我没有收到声音。 那么问题是什么呢?因为文件正在生成但播放时没有发出声音。 这是我的代码

func createRecorder() -> () {

    let currentDate = NSDate()
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yy HHmmss"
    let fileName = "Recording on " + dateFormatter.string(from: currentDate as Date) + ".wav"
    let filePaths = NSSearchPathForDirectoriesInDomains(.musicDirectory, .userDomainMask, true)
    if let firstPath = filePaths.first {


        let recordingPath = "\(firstPath)/\(fileName)"
        let url = NSURL(fileURLWithPath: recordingPath)
        let selectedPreset = RecordingPreset.High
        do {
            recorder = try AVAudioRecorder(url: url as URL, settings: selectedPreset.settings())
        }catch {
            print("nope")
        }
        recorder!.isMeteringEnabled = true

    }

}



@IBAction func btnRecordPressed(_ sender: Any) {
    createRecorder()
     recorder!.prepareToRecord()
    recorder?.record()

}

@IBAction func btnStopPressed(_ sender: Any) {
    recorder?.stop()
}

}

这是录音设置

enum RecordingPreset: Int {
    case Low = 0
    case Medium
    case High

    func settings() -> Dictionary<String, Int> {
        switch self {
        case .Low:
            return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 12_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]

        case .Medium:
            return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 24_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]

        case .High:
            return [AVLinearPCMBitDepthKey: 16, AVNumberOfChannelsKey : 1, AVSampleRateKey : 48_000, AVLinearPCMIsBigEndianKey : 0, AVLinearPCMIsFloatKey : 0]
        }
    }

    func exportSettings() -> Dictionary <String, Int> {

        var recordingSetting = self.settings()
        recordingSetting[AVFormatIDKey] = Int(kAudioFormatLinearPCM)
        recordingSetting[AVLinearPCMIsNonInterleaved] = 0

        return recordingSetting
    }
}

0 个答案:

没有答案