swift - Play sound with AKAudioPlayer - iOS -


how declare akaudioplayer?

i'm using audiokit lib , need play .wav file change file button.

    import uikit     import audiokit      class viewcontroller: uiviewcontroller {              let file   = nsbundle.mainbundle().pathforresource("song", oftype: "wav")             let song  = akaudioplayer(file!) // <--- error = instance member 'file' cannot used on type               override func viewdidload() {                 super.viewdidload()                  audiokit.output = song                 audiokit.start()                  song.play()             }               @ibaction func btn(sender: anyobject) {                  song.replacefile("newfile")                 song.play()              }          } 

this fast solution problem. can done better @ least can idea.

first try make new class function play file , function reload new replacement file this.

class playmymusic {   var songfile = nsbundle.mainbundle()   var player: akaudioplayer!    func play(file: string, type: string) -> akaudioplayer {     let song = songfile.pathforresource(file, oftype: type)     player = akaudioplayer(song!)     return player   }    func replay(file: string, type: string, curplay: akaudioplayer) {     let song = songfile.pathforresource(file, oftype: type)     curplay.stop()     curplay.replacefile(song!)     curplay.play()   } } 

initiate class inside view

class testviewcontroller: uiviewcontroller {     let doplay = playmymusic().play("a", type: "wav")    .........    ......... 

play music inside view

override func viewdidload() {     super.viewdidload()      audiokit.output = self.doplay     audiokit.start()     doplay.looping = true     doplay.play()  } 

then when want reload new file use replay function

@ibaction func btn(sender: anyobject) {     playmymusic().replay("c", type: "wav", curplay: self.doplay)  } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -