Play a sound when bluetooth is ON : no sound (iOS Swift) -
i programming bluetooth sensor ios swift, , play sound when bluetooth connected , sensor working. example, when temperature high, sound alert should beep.
however, reason, sound silent though playsound() function seems working : see println message "playing audio (test line. did line appear in console ?)" console.
i wonder whether bluetooth block avaudioplayer working. how make sound ?
the code such programmed sound should beep long bluetooth on. noticed interesting phenomenon : at moment when bluetooh turned off, sound beeps once. @ moment when app put background (push iphone home button), sound beeps once. kind of have feeling bluetooth blocked avaudioplayer, therefore, @ moment bluetooth turned off, avaudioplayer grabbed chance work once.
thanks bill
func playsound(){ println("playing audio (test line. did line appear in console ?)") // set sound file name & extension var alertsound = nsurl(fileurlwithpath: nsbundle.mainbundle().pathforresource("mario", oftype: "mp3")!) // preperation avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) avaudiosession.sharedinstance().setactive(true, error: nil) // play sound var error: nserror? audioplayer = avaudioplayer(contentsofurl: alertsound, error: &error) audioplayer.preparetoplay() audioplayer.play() } func peripheral(peripheral: cbperipheral!, didupdatevalueforcharacteristic characteristic: cbcharacteristic!, error: nserror!) { //self.statuslabel.text = "connected" if characteristic.uuid == irtemperaturedatauuid { // read parameters let allvalues = sensor.gettemperaturedata(characteristic.value) self.temperaturedata = allvalues[0] ////////////////////////////////////////////////////////////////// self.playsound() }
Comments
Post a Comment