ios - Initializing AVAudioPlayer to be used in more than one function -


usual avaudioplayer tutorials online creates avaudioplayer within function play , stop functions of avaudioplayer object aren't available object directly other functions. problem function stop sound avaudioplayer. seems pretty simple initializing objects @ top of class in hopes accessible in swift3 init function avaudioplayer includes throw , parameter sound file. swift doesn't allow use instance member within property initializer i'm stuck on thought process of how written.

the error i'm running @ point not being allowed use instance member in property initializer when creating "backgroundmusicplayer":

import uikit import avfoundation  class madliboneviewcontroller: uiviewcontroller, uitextfielddelegate {      @iboutlet weak var theplace: uitextfield!     @iboutlet weak var theverb: uitextfield!     @iboutlet weak var thenumber: uitextfield!     @iboutlet weak var thetemplate: uitextview!     @iboutlet weak var thestory: uitextview!     @iboutlet weak var generatestorybutton: uibutton!     @iboutlet weak var proceedtonextmadlib: uibutton!     //var backgroundmusicplayer = avaudioplayer()      var error:nserror?      var path = bundle.main.path(forresource: "bensound-cute", oftype: "mp3")      var url: nsurl {         return nsurl(fileurlwithpath: path!)     }      var backgroundmusicplayer: avaudioplayer = try avaudioplayer(contentsof: url url, error: &error)          @ibaction func createstory(_ sender: anyobject) {         thestory.text=thetemplate.text     thestory.text=thestory.text.replacingoccurrences(of: "<place>", with: theplace.text!)         thestory.text=thestory.text.replacingoccurrences(of: "<verb>", with: theverb.text!)         thestory.text=thestory.text.replacingoccurrences(of: "<number>", with: thenumber.text!)          generatestorybutton.ishidden=true         proceedtonextmadlib.ishidden=false      }      @ibaction func shownextstory(_ sender: anyobject) {          view.backgroundcolor=uicolor.green          let storyboard : uistoryboard = uistoryboard(name: "main", bundle:nil)         let resultviewcontroller = storyboard.instantiateviewcontroller(withidentifier: "madlibtwoviewcontroller") as! madlibtwoviewcontroller         self.present(resultviewcontroller, animated:true, completion:nil)      }      @ibaction func hidekeyboard(_ sender: anyobject) {         theplace.resignfirstresponder()         theverb.resignfirstresponder()         thenumber.resignfirstresponder()         thetemplate.resignfirstresponder()     }     override func viewdidload() {         super.viewdidload()         proceedtonextmadlib.ishidden=true         view.backgroundcolor = uicolor.purple           // additional setup after loading view.         self.theverb.delegate = self         self.theplace.delegate = self         self.thenumber.delegate = self      }      func textfieldshouldreturn(_ textfield: uitextfield) -> bool {         self.view.endediting(true)         return false     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }  } 

you need use lazy initialisation/instantiation that. in case need do.

lazy var player: avaudioplayer = {     [unowned self] in     {         return try avaudioplayer.init(contentsof: self.url)     }     catch {         return avaudioplayer.init()     }  }() 

for more lazy initialisation this read. interesting thing in case initialiser throws. think this forum discussion helpful have slight idea.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -