0
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.wav")

synthesizer.write(utterance) { buffer in
           guard let pcmBuffer = buffer as? AVAudioPCMBuffer else {
              return
           }
           if pcmBuffer.frameLength == 0 {
              // no length
           }else{
              if output == nil {
                  let SAMPLE_RATE =  Float64(16000.0)

                  let outputFormatSettings = [
                      AVFormatIDKey:kAudioFormatLinearPCM,
                      AVLinearPCMBitDepthKey:16,
                      AVLinearPCMIsFloatKey: false,
                      AVSampleRateKey: SAMPLE_RATE,
                      AVNumberOfChannelsKey: 1
                      ] as [String : Any]
                  print(pcmBuffer.format.settings)
                 output = try! AVAudioFile(forWriting: fileURL, settings: outputFormatSettings, commonFormat: .pcmFormatInt16, interleaved: false)
                  
              }
               
              try! output!.write(from: pcmBuffer)
           }
        }

afinfo输出:

File type ID:   WAVE
Num Tracks:     1
----
Data format:     1 ch,  16000 Hz, Int16
                no channel layout.
estimated duration: 0.000000 sec
audio bytes: 0
audio packets: 0
bit rate: 256000 bits per second
packet size upper bound: 2
maximum packet size: 2
audio data file offset: 4096
not optimized
source bit depth: I16

为什么估计的持续时间为零?

我可以导出 wav 文件,但无法在 AVAudioPlayer 中播放。VLC 播放器可以播放生成的文件。我的代码有什么问题?

4

0 回答 0