How to play sound using AVAudioPCMBuffer

I cannot play sound using AVAudioPCMBuffer (although I could play with AVAudioFile). I got this error.

ERROR: AVAudioBuffer.mm:169: - [AVAudioPCMBuffer initWithPCMFormat: frameCapacity:]: required false: isCommonFormat

here is my code below and i am very grateful for your help.

import UIKit import AVFoundation class ViewController: UIViewController { let audioEngine: AVAudioEngine = AVAudioEngine() let audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. audioEngine.attachNode(audioFilePlayer) let filePath: String = NSBundle.mainBundle().pathForResource("test", ofType: "mp3")! let fileURL: NSURL = NSURL(fileURLWithPath: filePath)! let audioFile = AVAudioFile(forReading: fileURL, error: nil) let audioFormat = audioFile.fileFormat let audioFrameCount = UInt32(audioFile.length) let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount) var mainMixer = audioEngine.mainMixerNode audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format) audioFilePlayer.scheduleBuffer(audioFileBuffer, atTime: nil, options: nil, completionHandler: nil) var engineError: NSError? audioEngine.startAndReturnError(&engineError) audioFilePlayer.play() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+5
source share
2 answers

just let me share, it worked somehow, although I don't fully understand.

 import UIKit import AVFoundation class ViewController: UIViewController { var audioEngine: AVAudioEngine = AVAudioEngine() var audioFilePlayer: AVAudioPlayerNode = AVAudioPlayerNode() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let filePath: String = NSBundle.mainBundle().pathForResource("test", ofType: "mp3")! println("\(filePath)") let fileURL: NSURL = NSURL(fileURLWithPath: filePath)! let audioFile = AVAudioFile(forReading: fileURL, error: nil) let audioFormat = audioFile.processingFormat let audioFrameCount = UInt32(audioFile.length) let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount) audioFile.readIntoBuffer(audioFileBuffer, error: nil) var mainMixer = audioEngine.mainMixerNode audioEngine.attachNode(audioFilePlayer) audioEngine.connect(audioFilePlayer, to:mainMixer, format: audioFileBuffer.format) audioEngine.startAndReturnError(nil) audioFilePlayer.play() audioFilePlayer.scheduleBuffer(audioFileBuffer, atTime: nil, options: nil, completionHandler: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+5
source

The problem is that you are setting the PCM buffer format to a format other than PCM. Therefore, you need to create your AVAudioPCMBuffer using AVAudioFile processingFormat .

+2
source

Source: https://habr.com/ru/post/1205933/


All Articles