ios - Add sound for each button -
i'm making app name user , choose thumbnail (and it'll play sound). problem couldn't play different sounds each button. plays last 1 keyed value in (in case, avengers). started swift last week , don't know language. google of time. here's code. in advance.
import uikit import avfoundation class imagesviewcontroller: uiviewcontroller, avaudioplayerdelegate { var audioplayer: avaudioplayer? @ibaction func antman(sender: anyobject) { audioplayer!.play() } @ibaction func avengers(sender: anyobject) { audioplayer!.play() } func selectsound(alertsound: string, sound: string) { var alertsound: nsurl = nsurl (fileurlwithpath: nsbundle.mainbundle().pathforresource(sound, oftype: "mp3")!) var error: nserror? { audioplayer = try avaudioplayer(contentsofurl: alertsound) } catch var error1 nserror { error = error1 audioplayer = nil } audioplayer!.delegate = self audioplayer!.preparetoplay() } override func viewdidload() { super.viewdidload() // additional setup after loading view. selectsound("1", sound: "antman") selectsound("2", sound: "avengers") } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func audioplayerdidfinishplaying(player: avaudioplayer, flag: bool) { } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { let newvc: results = segue.destinationviewcontroller as! results print("xxx ,%s", segue.identifier) newvc.receivedfirstname = segue.identifier! } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ }
you can like:
@ibaction func antman(sender: anyobject) { selectsound("1", sound: "antman") audioplayer!.play() } @ibaction func avengers(sender: anyobject) { selectsound("2", sound: "avengers") audioplayer!.play() }
Comments
Post a Comment