简体   繁体   English

如何在UITableViewCell(Swift)中暂停AVPlayer?

[英]How can I pause AVPlayer in UITableViewCell (Swift)?

I am broadcasting stations in a table and am trying to figure out how to pause the station when the cell is clicked. 我正在用表格广播电台,并试图弄清楚如何在单击该单元格时暂停该电台。 I can get it to play but not pause. 我可以播放但不能暂停。 Also is there anyway to indicate that the current cell is playing. 无论如何,也有指示当前正在播放的单元格。 I am wondering because I am not able to access my custom cell outside of the cellforindexpath function. 我在想,因为我无法在cellforindexpath函数之外访问我的自定义单元。 Here is my code. 这是我的代码。

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 has a rate property that = 0.0 when it is not playing and <= 1.0 when it is playing. AVPlayer的rate属性在不播放时= 0.0,在播放时<= 1.0。

If you only want one broadcasting station to play at once, you can instantiate an AVPlayer in a singleton class, and change the AVPlayerItem based on what station is playing. 如果只希望一次播放一个广播电台,则可以在单例类中实例化一个AVPlayer,然后根据正在播放的电台来更改AVPlayerItem。 In this way, you will only ever have one instance of an AVPlayer, and instead of checking if that custom cell is playing, you can check if the AVPlayer itself is playing. 这样,您将只有一个AVPlayer实例,并且可以检查AVPlayer本身是否在播放,而不是检查该自定义单元是否正在播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM