gpt4 book ai didi

ios - 单击单元格中的按钮时如何解析以播放某首歌曲?

转载 作者:行者123 更新时间:2023-11-30 14:03:45 27 4
gpt4 key购买 nike

当我播放歌曲时,它会记录在我的应用程序中。歌曲标题、艺术家等列在单元格中。每次我转到下一首歌曲时,都会添加一个新单元格。目前,每个单元格中都列出了标题和艺术家以及一个按钮。每当我单击该按钮时,我的程序都会检查该歌曲是否正在解析,如果是,它将播放该文件。 IE。如果选择歌曲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)

最佳答案

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

看看这个。 Detecting which UIButton was pressed in a UITableView

关于ios - 单击单元格中的按钮时如何解析以播放某首歌曲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com