Avaudioplayer when playing a MP3 file, there was no sound. MP3 files are placed in the project, the path is not wrong, but there is no sound.
func PlaySound () {Let Notifyurl = Nsbundle.mainbundle (). Pathforresource ("Noti FY ", OfType:" MP3 ")//let Notifyurl = Nsbundle.mainbundle (). ResourcePath?. stringByAppendingPathComponent ("Notify.mp3") if let mp3 = notifyurl {Let URL = Nsurl (fileurlwithpath:m P3)//var FileManager = Nsfilemanager ()//Let result = Filemanag Er.fileexistsatpath (MP3)//println ("result=\ (Result)") avaudiosession.shared Instance (). Setcategory (Avaudiosessioncategoryplayback, Error:nil) var error:nserror? var avaudioplayer = Avaudioplayer (contentsofurl:url, Error: &ERROR) if error! = Nil {Prin TLN (Error)} else {Avaudioplayer.volume = 1.0 avaudioplayer.preparetoplay () Avaudioplayer.play ()}}}
The solution is to have a sound when you define Avaudioplayer as a global variable, isn't it amazing.
var avaudioplayer:avaudioplayer?func playSound () {Let Notifyurl = Nsbundle.mainbundle (). Pathforresource ("Notify", OfType: "MP3")//let Notifyurl = Nsbundle.mainbundle (). ResourcePath?. stringByAppendingPathComponent ("Notify.mp3") if let mp3 = notifyurl {Let URL = Nsurl (fileurlwithpath:m P3)//var FileManager = Nsfilemanager ()//Let result = Filemanag Er.fileexistsatpath (MP3)//println ("result=\ (Result)") avaudiosession.shared Instance (). Setcategory (Avaudiosessioncategoryplayback, Error:nil) var error:nserror? var avaudioplayer = Avaudioplayer (contentsofurl:url, Error: &error) Avaudioplayer = Avaudioplayer (conten Tsofurl:url, Error: &ERROR) if error! = Nil {println (Error)} else { Avaudioplayer?. Volume = 1.0 avaudioplayer?. PreparetoPlay () Avaudioplayer?. Play ()}}}
Avaudioplayer no sound when playing audio files