I have a WatchKit app that, when the button is used on the watch, signals the iOS app to play sound. For some reason, the sound does not play when I use my own class to process the instance AVAudioPlayerand play the sound. If I do this part in session:didReceiveMessage:replyHandler:, it works great.
Here is a custom class:
import AVFoundation
class ChildClass {
let mySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test", ofType: "wav")!)
var audioPlayer: AVAudioPlayer!
func playSound() {
do {
print("Playing sound")
self.audioPlayer = try AVAudioPlayer(contentsOfURL: mySound)
self.audioPlayer.play()
} catch {
print("Error getting audio file")
}
}
}
which is created in my ViewController:
class ViewController: UIViewController, WCSessionDelegate {
var session: WCSession!
override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let mySounds = ChildClass()
mySounds.playSound()
}
}
Setting breakpoints in ChildClass.playSound()I can confirm that it is called. But not only the sound is not reproduced, but print()also does not output anything to the console.
Is there something in the AVAudioPlayerinstance in the user class, causing it to not play sound? Or, perhaps, the sound is not played by the iOS application for any reason?