Hallo zusammen!
Ich möchte eine Audiodatei abspielen und den Pitch während des Abspielens verändern können. Daher greife ich auf AVAudioPlayerNode mit verknüpfter AVAudioUnitTimePitch Control zurück, das verändern des Pitch funktioniert auch. Allerdings weiß ich nicht wie ich die Datei nun loopen kann. Mein Versuch lief über AVAudioPCMBuffer. Hier mein Code:
Alles anzeigen
Allerdings kommt bei Programmstart ein Fehler: Thread 1: "required condition is false: isPCMFormat" aufgrund
Ich möchte eine Audiodatei abspielen und den Pitch während des Abspielens verändern können. Daher greife ich auf AVAudioPlayerNode mit verknüpfter AVAudioUnitTimePitch Control zurück, das verändern des Pitch funktioniert auch. Allerdings weiß ich nicht wie ich die Datei nun loopen kann. Mein Versuch lief über AVAudioPCMBuffer. Hier mein Code:
Quellcode
- func playSound(soundName: String) throws{
- guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else { return }
- let file = try AVAudioFile(forReading: url)
- let audioPlayer = AVAudioPlayerNode()
- //New:
- let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: file.fileFormat, frameCapacity: AVAudioFrameCount(file.length))
- try? file.read(into: audioFileBuffer!)
- engine.attach(audioPlayer)
- engine.attach(pitchControl)
- engine.connect(audioPlayer, to: pitchControl, format: nil)
- engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
- audioPlayer.scheduleBuffer(audioFileBuffer!,at: nil, options: .loops, completionHandler: nil) //Changed
- try engine.start()
- audioPlayer.play()
- }