【问题标题】:Channels AVAudioRecorder Swift 3频道 AVAudioRecorder Swift 3
【发布时间】:2017-01-18 23:14:45
【问题描述】:

在互联网上苦苦搜索之后,我似乎仍然无法找到这样一个简单问题的答案。我正在使用 avaudiorecorder 录制音频,由于我的录音是“单声道”,因此我目前的音频会话设置中有一个通道。我的设置如下所示:

try session.setCategory(AVAudioSessionCategoryAmbient)
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
try session.setActive(true)
let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]
audioRecorder = try AVAudioRecorder(url: audioFileName, settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true

而我想做的只是以下

audioRecorder.averagePower(forChannel: 1)

由于某种原因,我总是得到

fatal error: unexpectedly found nil while unwrapping an Optional value

顺便说一句,使用模拟器时会出现这种情况......即使我尝试播放刚刚录制的声音,它也没有问题。

有人知道为什么吗?

非常感谢!

【问题讨论】:

    标签: swift audio audio-recording avaudiorecorder


    【解决方案1】:

    与索引一样,第一个通道是0。只需使用:

    audioRecorder.averagePower(forChannel: 0)
    

    【讨论】:

      【解决方案2】:

      你只有一个频道

      所以用这个:

      audioRecorder.updateMeters()
      let normalizedValue = pow(10, audioRecorder.averagePower(forChannel: 0) / 20)
      

      【讨论】:

      • 这里重要的部分是通道0
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多