5

사운드를 재생하는 방법을 나는 AVAudioPCMBuffer 사운드를 재생할 수 없습니다. 이 오류가 발생했습니다.AVAudioPCMBuffer

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

여기

아래에있는 내 코드가, 나는 정말 당신의 도움에 대한 감사하겠습니다 : 필요 조건이 거짓입니다.

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

이 항목을 공유하도록하겠습니다. 완전히 이해할 수는 없지만 어떻게 든 작동했습니다.

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. 
} 

} 
2

문제는 PCM 버퍼의 형식을 비 PCM 형식으로 설정하는 것입니다. 따라서 AVAudioFile의 processingFormat으로 AVAudioPCMBuffer을 만들어야합니다.

관련 문제