Skip to content

Instantly share code, notes, and snippets.

@DarrellBrogdon
Created January 28, 2016 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrellBrogdon/31acf617fbcd303b7db8 to your computer and use it in GitHub Desktop.
Save DarrellBrogdon/31acf617fbcd303b7db8 to your computer and use it in GitHub Desktop.
Audio Test
import WatchKit
import Foundation
import WatchConnectivity
//
// The WatchKit Interface Controller
//
class InterfaceController: WKInterfaceController, WCSessionDelegate {
var session: WCSession!
@IBAction func sendMessage() {
let messageToSend = ["Value": "Play Sound"]
session.sendMessage(messageToSend, replyHandler: {replyMessage in
print("Sending the message")
}, errorHandler: {error in
print(error)
})
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if WCSession.isSupported() {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
import Foundation
import AVFoundation
//
// A class for playing the sound
//
class TestSound {
let theSoundFile = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "wav")!)
var audioPlayer: AVAudioPlayer!
func playSound() {
print("Playing test sound")
do {
self.audioPlayer = try AVAudioPlayer(contentsOfURL: theSoundFile)
self.audioPlayer.play()
} catch {
print("Unable to find sound file")
}
}
}
import UIKit
import WatchConnectivity
//
// The iOS View Controller
//
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var messageLabel: UILabel!
var session: WCSession!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if WCSession.isSupported() {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let value = message["Value"] as? String
let testSound = TestSound()
testSound.playSound()
dispatch_async(dispatch_get_main_queue(), {
self.messageLabel.text = value
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment