单击单元格中的按钮时,如何解析播放某首歌曲? [英] How do I get parse to play a certain song when a button in the cell is clicked?

查看:12
本文介绍了单击单元格中的按钮时,如何解析播放某首歌曲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我播放歌曲时,它会记录在我的应用中.歌曲名称、艺术家等列在一个单元格中.每次我转到下一首歌曲时,都会添加一个新单元格.目前在每个单元格中都列出了标题和艺术家以及一个按钮.每当我单击按钮时,我的程序都会检查该歌曲是否在解析中,如果是,它将播放该文件.IE.如果选择歌曲 a,它将检查歌曲 a 是否在解析中,如果是,它将播放该文件,与歌曲 b 相同,但如果我返回并单击第一首歌曲的播放按钮,它将播放第二首歌曲.如何让每个单元格中的每个按钮播放该单元格中的相应歌曲?

When I play song it gets recorded in my app. The song title, artist, etc. is listed in a cell. Every time I go to the next song a new cell is added. Currently in each cell the title and artist are listed as well as a button. Whenever I click the button my program will check if that song is in parse and if it is it will play that file. I.e. if song a is chosen it will check if song a is in parse and if it is it will play that file, same with song b, but if I go back and click the play button for the first song it plays the second song. How do I get each button in each cell to play the corresponding song in that cell?

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)

推荐答案

听起来问题出在您的 tableview 和单元格的重用中.您如何确定按下的按钮与哪个单元格索引相关联?

Sounds like the issue is in your tableview and re-use of the cells. How are you determining what cell index a button press is associated with?

看看这个.检测在 UITableView 中按下了哪个 UIButton

这篇关于单击单元格中的按钮时,如何解析播放某首歌曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆