So I have been struggling to get AVAudioEngine to repeat a sound for me. I need it to play a track (about 26 seconds) over and over again. It needs to be using AVAudioEngine, not AVAudioPlayer, because I need to be able to change it's tone. I've found examples of changing tone, and forcing the sound to repeat, but only for sine waves, links below.
My old code (before trying to implement the problem) is below. It will play the sound through once when the button is pushed, but doesn't do it again.
import UIKit
import AVFoundation
class aboutViewController: UIViewController {
var audioUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!)
var audioEngine = AVAudioEngine()
var myPlayer = AVAudioPlayerNode()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
audioEngine.attachNode(myPlayer)
var audioFile = AVAudioFile(forReading: audioUrl, error: nil)
var audioError: NSError?
audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)
myPlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(&audioError)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func toggleSideMenu(sender: AnyObject) {
toggleSideMenuView()
}
@IBAction func testSound(sender: AnyObject) {
myPlayer.play()
}
}
My code attempt to implement the repeating is below, and this doesn't do anything (doesn't crash).
import UIKit
import AVFoundation
class aboutViewController: UIViewController {
var audioUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("chimes", ofType: "wav")!)
var audioEngine = AVAudioEngine()
var myPlayer = AVAudioPlayerNode()
var audioError: NSError?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
audioEngine.attachNode(myPlayer)
var buffer = AVAudioPCMBuffer(PCMFormat: myPlayer.outputFormatForBus(0),frameCapacity:100)
buffer.frameLength = 100
var mixer = audioEngine.mainMixerNode
var audioFile = AVAudioFile(forReading: audioUrl, error: nil)
audioEngine.connect(myPlayer, to: audioEngine.mainMixerNode, format: audioFile.processingFormat)
audioEngine.startAndReturnError(&audioError)
myPlayer.play()
myPlayer.scheduleBuffer(buffer, atTime: nil, options:.Loops , completionHandler: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func testSound(sender: AnyObject) {
}
}
So what I am asking is this: Can you put together an example that plays an audio file over and over, and have a variable at the top (ex: let tone:Int = 100), that sets the tone that the audio file will be repetitively played at.
Thanks for your help!
Links:
Example of a repeating sine wave: http://www.tmroyal.com/playing-sounds-in-swift-audioengine.html
Example of change in tone: Using sound effects with AudioEngine