AVAudioPlayerNode不播放声音 [英] AVAudioPlayerNode does not play sound

查看:151
本文介绍了AVAudioPlayerNode不播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AudioPlayerNode是一个实例变量,代码如下:

The AudioPlayerNode is an instance variable code is a follows:

class HXAudioEngine {
   private var audioEngine: AVAudioEngine = AVAudioEngine()

   var digitFileUrl: URL? {
      didSet {
          if let digitUrl = digitFileUrl {
              do {
                  digitAudioFile = try AVAudioFile(forReading: digitUrl)
              } catch let error {
                print("Error loading Digit file: \(error.localizedDescription)")
            }
          }
      }
   }
   var digitAudioFile: AVAudioFile? {
      didSet {
          if let digitFile = digitAudioFile {
              digitAudioFormat = digitFile.processingFormat
              digitFileBuffer = AVAudioPCMBuffer(pcmFormat: digitFile.processingFormat, frameCapacity: UInt32(digitFile.length))
          }
      }
   }
   var digitFileBuffer: AVAudioPCMBuffer? {
      didSet {
          if let buffer = digitFileBuffer {
              do {
                  try digitAudioFile?.read(into: buffer)
              } catch let error {
                  print("Error loading digit file into buffer: \(error.localizedDescription)")
              }
          }
      }
  }
  var digitAudioFormat: AVAudioFormat?
  var digitPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

  func playDigit() {
      let file = "d0p1m00db"
      digitFileUrl = Bundle.main.url(forResource: file, withExtension: "wav")
      audioEngine.attach(digitPlayer)
      audioEngine.connect(digitPlayer, to: audioEngine.mainMixerNode, format: digitAudioFormat)
      audioEngine.prepare()

      do {
           try audioEngine.start()
      } catch let error {
           print(error)
      }

      guard let digitBuffer = digitFileBuffer else { return }
      guard let digitAudioFile = digitAudioFile else { return }

      digitPlayer.scheduleBuffer(digitBuffer, at: nil, options: .interrupts) {
             print("Done playing digit")
      }
      digitPlayer.play()
   }
}

由于某种原因,最终被播放的文件只是一个喀哒声.音频文件是一位女士说1位数字的1秒长文件.

For some reason the file that ends up being played is just a click sound. The audio file is a 1 second long file of a lady speaking one digit.

播放器似乎加快了声音文件的速度,因此掩盖了语音.采样率为44100.阅读Stack Overflow时,有很多解决方案都在讨论在声音文件完成之前将音频播放器释放的方法.

It almost seems like the player speeds through the sound file and hence obscures the speech. The sample rate is 44100. Reading on Stack Overflow there is alot of solutions talking about the audioplayer being deallocated before the sound file is completed.

但是我已经测试了通过Raywenderlich教程构建的单独应用程序:

But I have tested on a separate app built through Raywenderlich tutorial: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started, which actually shows the length of the sound file and the playing progress. And all seems fine but even then the audio file doesn't play properly.

我的实现是否存在问题,或者是音频文件?音频文件在iTunes上可以正常播放.

Is there an issue with my implementation or is it the audio file? The audio file plays fine on itunes.

推荐答案

如果您需要用于播放的输出设备,则必须将耳机或其他类型的选择设置为音频会话类别.

if you need for output device for playing , headphone or other kind of choices must be set audiosession category.

try AVAudioSession.sharedInstance().setCategory(.playAndRecord , mode: .default , options: .defaultToSpeaker)

这篇关于AVAudioPlayerNode不播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆