如何从Parse制作音乐我上传了播放洗牌?

时间:2016-01-03 18:40:45

标签: ios arrays swift parse-platform shuffle

这是我到目前为止所做的:

              let query = PFQuery(className: "Genres")
              //Find objects in the background
               query.findObjectsInBackgroundWithBlock({
        //store objects in an array
        (objectsArray :[PFObject]?, error: NSError?) -> Void in

        let objectIDs = objectsArray
        // objects being added to array
        for i in 0...objectIDs!.count-1{
        // add a new element in the array
        self.iDArray.append(objectIDs![i].valueForKey("objectId") as! String)
        //store song name in song array
        self.NameArray.append(objectIDs![i].valueForKey("SongName")as! String)
        self.tableView.reloadData()

        NSLog("\(objectIDs)")

        }

    })
}

func grabSong () {
    let songQuery = PFQuery(className: "Genres")
    songQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block: {
        (object: PFObject?, error : NSError?) -> Void in

        if let audioFile = object?["SongFile"] as? PFFile {
            let audioFileUrlString: String = (audioFile.url)!
            let audioFileUrl = NSURL(string: audioFileUrlString)!
            AudioPlayer = AVPlayer(URL: audioFileUrl)
            AudioPlayer.play()
        }
    })
}

1 个答案:

答案 0 :(得分:0)

一种可能的方法(基于您的代码)

  1. 随机播放iDArray(如何对数组进行洗牌很容易用Google搜索。)
  2. 加载idArray后将SelectedSongNumber设置为0
  3. 然后,每当您致电grabSong时,请将SelectedSongNumber增加1
  4. if SelectedSongNumber< iDArray.count继续加载歌曲,否则您已完成
  5. 观察AVPlayer,当它完成播放项目时,再次致电grabSong(如何在SO上观察播放器)
  6. 应该改变你喜欢的一切:)