簡體   English   中英

單擊單元格中的按鈕時,如何解析播放某首歌曲?

[英]How do I get parse to play a certain song when a button in the cell is clicked?

當我播放歌曲時,它會記錄在我的應用程序中。 歌曲名稱、藝術家等列在一個單元格中。 每次我轉到下一首歌曲時,都會添加一個新單元格。 目前在每個單元格中都列出了標題和藝術家以及一個按鈕。 每當我單擊按鈕時,我的程序都會檢查該歌曲是否在解析中,如果是,它將播放該文件。 即,如果選擇歌曲 a,它將檢查歌曲 a 是否在解析中,如果是,它將播放該文件,與歌曲 b 相同,但如果我返回並單擊第一首歌曲的播放按鈕,它將播放第二首歌曲。 如何讓每個單元格中的每個按鈕播放該單元格中的相應歌曲?

func playit(sender: UIButton!){
    if let nowPlaying = musicPlayer.nowPlayingItem{
    let title = nowPlaying[MPMediaItemPropertyTitle] as? String
    let artist = nowPlaying[MPMediaItemPropertyTitle] as? String

    println(title! + artist!)


        let query = PFQuery(className: "Songs")
        query.whereKey("SongName", equalTo: title!)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) song(s).")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)
                        let playButtonrow = sender.tag
                        let object = object as PFObject
                        let parseAudio = object.valueForKey("SongFile") as! PFFile
                        let audioPath: String = parseAudio.url!
                        let urlParse: NSURL = NSURL(string: audioPath)!



                        player = AVPlayer(URL: urlParse)
                        println(player)
                        player.volume = 1.0
                        player.play()
                        if (player.rate > 0) && (player.error == nil) {
                            // player is playing
                            println("Playing")
                        } else {
                            println("Not Playing")
                        }
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }

        }

    }
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: UITableViewCell = self.table.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    var play: Play


    play = add[indexPath.row]
    let playButton : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    playButton.tag = indexPath.row
    let imageret = "playbutton"
    playButton.setImage(UIImage(named: imageret), forState: .Normal)
    playButton.frame = CGRectMake(236, 20, 100, 100)
    playButton.addTarget(self,action: "playit:", forControlEvents: UIControlEvents.TouchUpInside)

    for view: UIView in cell.contentView.subviews as! Array<UIView> {
        view.removeFromSuperview()
    }
    cell.contentView.addSubview(playButton)

聽起來問題出在您的表格視圖和單元格的重用中。 您如何確定按下的按鈕與哪個單元格索引相關聯?

看一下這個。 檢測在 UITableView 中按下了哪個 UIButton

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM