I have a WatchKit app that when a button is tapped on the watch it signals the iOS app to play a sound. For some reason, the sound isn't playing when I use a custom class to handle setting up an instance of AVAudioPlayer
and playing the sound. If I do that part within session:didReceiveMessage:replyHandler:
it plays fine.
Here is the 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 instantiated 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 up breakpoints in ChildClass.playSound()
I can confirm that it is getting called. But not only doesn't the sound play but the print()
doesn't appear to print anything to the console either.
Is there something about the AVAudioPlayer
instance being in a custom class causing it to not play the sound? Or, perhaps the audio isn't being played by the iOS app for some reason?