ios - AVAudioPlayer wont play sound when called in other swift file -
i have 2 swift files - viewcontroller:uiviewcontroller , audioplayer:avaudioplayer.
my audioplayer file has function
func seagullsound() { var tmp = avaudioplayer() var seagullsound = nsurl(fileurlwithpath: nsbundle.mainbundle().pathforresource("gulls", oftype: "mp3")!) avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) avaudiosession.sharedinstance().setactive(true, error: nil) var error:nserror? tmp = avaudioplayer(contentsofurl: seagullsound, error: &error) tmp.preparetoplay() tmp.play() println("this function got called!") }
i'm trying call function in viewcontroller thru tapping button, using code:
@ibaction func playsound(sender: anyobject) { var audio = audioplayer() audio.seagullsound() }
the sound not played when click button. however, print statement works. can audio play if move seagullsound() viewcontroller file, know mp3 work. haven't moved audio viewcontroller because want develop habit of not crowding code 1 file. in advance help.
edit:
class highscore: uiviewcontroller { var audioinitializer = audioplayer() func updatehighscore(score:int) -> string { nsuserdefaults.standarduserdefaults().integerforkey("highscore") //check if score higher nsuserdefaults stored value , change nsuserdefaults stored value if it's true if score > nsuserdefaults.standarduserdefaults().integerforkey("highscore") { //call applause sound audioinitializer.applausesound() //set score nsuserdefaults.standarduserdefaults().setinteger(score, forkey: "highscore") nsuserdefaults.standarduserdefaults().synchronize() } nsuserdefaults.standarduserdefaults().integerforkey("highscore") //use below line reset high score testing //nsuserdefaults.standarduserdefaults().removeobjectforkey("highscore") return string(nsuserdefaults.standarduserdefaults().integerforkey("highscore")) }}
here file sounds:
class audioplayer: avaudioplayer { var soundmaster = avaudioplayer() func tappingsound() { var tapsoundurl = nsbundle.mainbundle().urlforresource("tapsound", withextension: "mp3") avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) avaudiosession.sharedinstance().setactive(true, error: nil) var error:nserror? soundmaster = avaudioplayer(contentsofurl: tapsoundurl, error: &error) soundmaster.preparetoplay() soundmaster.play() } //need call in highscore.swift func applausesound() { var tapsoundurl = nsbundle.mainbundle().urlforresource("applause", withextension: "mp3") avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) avaudiosession.sharedinstance().setactive(true, error: nil) var error:nserror? soundmaster = avaudioplayer(contentsofurl: tapsoundurl, error: &error) soundmaster.preparetoplay() soundmaster.play() println("did called?") }}
you have move declaration of tmp avaudioplayer out of method. declare class variable.
you should use urlforresource instead of pathforresource:
let seagullsoundurl = nsbundle.mainbundle().urlforresource("gulls", withextension: "mp3")!
try this:
import uikit import avfoundation class highscore: uiviewcontroller { var audioplayer = avaudioplayer() override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func updatehighscore(score:int) -> string { //check if score higher nsuserdefaults stored value , change nsuserdefaults stored value if it's true if score > nsuserdefaults().integerforkey("highscore") { //call applause sound playaudio("applause") //set score nsuserdefaults().setinteger(score, forkey: "highscore") } //use below line reset high score testing //nsuserdefaults.standarduserdefaults().removeobjectforkey("highscore") return nsuserdefaults().integerforkey("highscore").description } func playaudio(audioname: string ) { var error:nserror? if let audiourl = nsbundle.mainbundle().urlforresource(audioname, withextension: "mp3") { audioplayer = avaudioplayer(contentsofurl: audiourl, error: &error) audioplayer.preparetoplay() audioplayer.play() } else if let error = error { println(error.description) } } @ibaction func playsound(sender: uibutton) { playaudio("gulls") } }
Comments
Post a Comment