[英]How can I pause AVPlayer in UITableViewCell (Swift)?
我正在用表格广播电台,并试图弄清楚如何在单击该单元格时暂停该电台。 我可以播放但不能暂停。 无论如何,也有指示当前正在播放的单元格。 我在想,因为我无法在cellforindexpath函数之外访问我的自定义单元。 这是我的代码。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell:BroadcastTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! BroadcastTableViewCell
// Configure the cell...
myCell.broadcaster.text = broadcasters[indexPath.row]
myCell.isBroadcasting.text = timePlayed[indexPath.row]
return myCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// self.performSegueWithIdentifier("toBroadcastView", sender: self)
self.player = AVPlayer(URL: NSURL(string: stations[indexPath.row]))
if selectedPath == indexPath.row{
if status == 0{
player.play()
status += 1
}else{
player.pause()
status -= 1
}
}else{
status = 0
player.play()
}
selectedPath = indexPath.row
}
AVPlayer的rate
属性在不播放时= 0.0,在播放时<= 1.0。
如果只希望一次播放一个广播电台,则可以在单例类中实例化一个AVPlayer,然后根据正在播放的电台来更改AVPlayerItem。 这样,您将只有一个AVPlayer实例,并且可以检查AVPlayer本身是否在播放,而不是检查该自定义单元是否正在播放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.