swift - Looping through music -
i have app want cycle through music. play 15 secs of song move on next one.
i have struct (musiclibrary) holds array of song information (a struct called songitem) including persistentid of songs on iphone. struct working , if print out names of songs works. (structs @ bottom of post)
i have function (playthesong) uses persistentid have stored in songs object mpmusicplayercontroller - part of mediaplayer library.
what happens when button pressed first song starts play. song persistentids printed debugger output , not sure why. after 15 seconds first song stops - added timer in attempt move onto next song, here doesn't work. think because loop through song structs executing , problem.
i know doing silly here, can't put finger on it.
note: work on phone simulator doesn't have music library.
this viewcontroller.
import uikit import mediaplayer class runningmusicviewcontroller: uiviewcontroller { var mymusic = musiclibrary() let mymusicplayer = mpmusicplayercontroller() var timer: int = 15 var clock = nstimer() override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func startmusicbutton(sender: anyobject) { // first song of our quiz , play - use persistent id. let songs = mymusic.getsongs() let totalsongs = songs.count clock = nstimer.scheduledtimerwithtimeinterval(1.0, target: self, selector: "countdown", userinfo: nil, repeats: true) index in 0..<totalsongs { playthesong(songs, index: index) } } func playthesong(songsinquiz: [songitem], index:int) { let songtoplay: songitem = songsinquiz[index] print("song id=" + string(songtoplay.persistentid)) let predicate = mpmediapropertypredicate(value: int(songtoplay.persistentid), forproperty: mpmediaitempropertypersistentid) let songquery = mpmediaquery() songquery.addfilterpredicate(predicate) mymusicplayer.setqueuewithitemcollection(songquery.collections![0]) mymusicplayer.play() } func countdown() { timer-- // when countdown hits 0 should stop song. if timer==0 { mymusicplayer.stop() clock.invalidate() timer = 15 } } }
this musiclibrary struct.
import foundation import mediaplayer struct musiclibrary { var musiclibrary: [songitem] = [] let query = mpmediaquery.songsquery() var mediacollection : mpmediaitemcollection { return mpmediaitemcollection(items: query.items!) } mutating func addsongs() { index in 0..<mediacollection.count { let mediaitem = mediacollection.items[index] let songitem = songitem( title: mediaitem.title!, album: mediaitem.albumtitle!, artist: mediaitem.artist!, artwork: mediaitem.artwork!, persistentid: mediaitem.persistentid, genre: mediaitem.genre! ) musiclibrary.append(songitem) } } func getsong(index: int)->songitem { return musiclibrary[index] } func getsongs()->[songitem] { return musiclibrary } }
and songitem struct
import foundation import mediaplayer struct songitem { var title: string var album: string var artist: string var artwork: mpmediaitemartwork var persistentid: uint64 var genre: string init(title: string, album: string, artist: string, artwork: mpmediaitemartwork, persistentid: uint64, genre: string) { self.title = title self.album = album self.artist = artist self.artwork = artwork self.persistentid = persistentid self.genre = genre } }
i change view controller along these lines:
import uikit import mediaplayer class runningmusicviewcontroller: uiviewcontroller { var index = 0 var timer: nstimer let mymusic = musiclibrary() let mymusicplayer = mpmusicplayercontroller() let numsecondstoplay = 15.0 let songs = mymusic.getsongs() @ibaction func startmusicbutton(sender: anyobject) { playthesong(index: 0) timer = nstimer.scheduledtimerwithtimeinterval(numsecondstoplay, target: self, selector: "playnextsong", userinfo: nil, repeats: true) } func playthesong(index: int) { let songtoplay: songitem = songs[index] print("song id=" + string(songtoplay.persistentid)) let predicate = mpmediapropertypredicate(value: int(songtoplay.persistentid), forproperty: mpmediaitempropertypersistentid) let songquery = mpmediaquery() songquery.addfilterpredicate(predicate) mymusicplayer.setqueuewithitemcollection(songquery.collections![0]) mymusicplayer.play() } func playnextsong() { mymusicplayer.stop() if index < songs.count { playthesong(index: index) index += 1 } else { timer.invalidate() } } }
Comments
Post a Comment