Avaudioplayer no sound when playing audio files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.