I am trying to build a simple mp3 player using AVAudioEngine
.
I made one using AVAudioPlayer
and it worked fine (eventually).
I now need to move up to AVAudioEngine
to give more functionality and I cannot get it to play even a simple file.
I have used exactly the code described in the Apple Documentation
And although the app builds (in Playgrounds at present) and prints all the messages to the console that I put in to show progress, nothing can actually be heard.
Here is the code I am using. Most from the Apple notes, but some from other questions I found online.
func azAEngine () {
let audioFile = try! AVAudioFile(forReading:URL(fileURLWithPath:Bundle.main.path(forResource:"bluesuedeshoes.mp3", ofType: nil)!)))
let azURL = URL(fileURLWithPath(Bundle.main.path(forResource:"bluesuedeshoes", ofType: "mp3")!))
print(azURL)
let audioEngine = AVAudioEngine()
let playerNode = AVAudioPlayerNode()
// Attach the player node to the audio engine.
audioEngine.attach(playerNode)
print("Audioengine attached ")
// Connect the player node to the output node.
audioEngine.connect(playerNode,
to: audioEngine.outputNode,
format: audioFile.processingFormat)
print("playernode attached")
playerNode.scheduleFile(audioFile,
at: nil,
completionCallbackType: .dataPlayedBack) { _ in
print("Done")
/* Handle any work that's necessary after playback. */
}
do {
try audioEngine.start()
playerNode.play()
print("playernode here ")
} catch {
/* Handle the error. */
print("Failed")
}
print("player started")
}