2014-10-31 14 views
5

Sound Ich kann Ton nicht mit AVAudioPCMBuffer spielen (obwohl ich mit AVAudioFile spielen könnte). Ich habe diesen Fehler erhalten.Wie spielt man mit AVAudioPCMBuffer

ERROR: AVAudioBuffer.mm:169: - [AVAudioPCMBuffer initWithPCMFormat: frameCapacity:]: erforderliche Bedingung ist falsch: isCommonFormat

hier ist mein Code unten, und ich würde wirklich für Ihre Hilfe zu schätzen wissen.

Antwort

5

nur lassen Sie mich teilen, das hat irgendwie funktioniert, obwohl ich nicht vollständig verstehe.

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

Das Problem ist, dass Sie das Format des PCM-Puffers auf ein Nicht-PCM-Format sind einstellen. Daher müssen Sie Ihre AVAudioPCMBuffer mit den AVAudioFile processingFormat erstellen.