AVPlayer在播放前停止UIActivityIndi​​cator

时间:2016-07-25 06:47:49

标签: ios swift swift2 avplayer

我试图在AVPlayer开始播放音乐时停止activityIndi​​cator,并在AVPlayer再次启动(加载,缓冲)时启动activityIndi​​cator。它有点工作,问题是AVPlayer在播放音乐之前几秒钟(5,6,7)之前停止activityIndi​​cator。而且当它再次(加载,缓冲)时它也不会再次启动activityIndi​​cator。任何人都知道我的错误或我需要解决的问题。感谢

var activityView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)

var selectIndex:Int = -1

var check = true
var url : String!
var playerItem:AVPlayerItem?
var player:AVPlayer?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RadioCollectionViewCell
    cell.backgroundColor = UIColor.yellowColor()

    let object = objects[indexPath.row]
    cell.img.image = UIImage(named: object["image"]!)
    cell.btnPlay.addTarget(self, action: Selector("audioControlButtonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
    cell.btnPlay.tag = indexPath.row+1


    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.item)!")
}

func audioControlButtonAction(sender: UIButton){

    if check == false {
        deallocObservers(player!)
    }



    var btn:NSInteger
    btn = sender.tag as NSInteger

    let object = objects[btn-1]
    let nurl = NSURL(string: "\(object["url"]!)")!
    playerItem = AVPlayerItem(URL: nurl)
    player=AVPlayer(playerItem: playerItem!)

    print(selectIndex)
    if selectIndex != -1 && selectIndex != sender.tag
    {
        let bt:UIButton = self.view.viewWithTag(selectIndex) as! UIButton

        if bt.selected == true
        {
            bt.selected = false
        }
    }

    if sender.selected == false{
        player!.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions(), context: nil)
        player!.addObserver(self, forKeyPath: "playbackBufferEmpty", options:NSKeyValueObservingOptions(), context: nil)
        player!.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options:NSKeyValueObservingOptions(), context: nil)
        player!.addObserver(self, forKeyPath: "playbackBufferFull", options:NSKeyValueObservingOptions(), context: nil)
        player!.addObserver(self, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions(), context: nil)
        player!.play()
        sender.selected = true
        check = false
        selectIndex = sender.tag
        activityView.startAnimating()


    }
    else{
        activityView.stopAnimating()
        check = true
        player?.pause()
        sender.selected = false
        selectIndex = -1
    }

    print(selectIndex)

}

func deallocObservers(player: AVPlayer) {

    player.removeObserver(self, forKeyPath: "status")
    player.removeObserver(self, forKeyPath: "playbackBufferEmpty")
    player.removeObserver(self, forKeyPath: "playbackLikelyToKeepUp")
    player.removeObserver(self, forKeyPath: "loadedTimeRanges")
    player.removeObserver(self, forKeyPath: "playbackBufferFull")

}


override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>){

    if object?.isEqual(player) == true && keyPath == "status" {

        print(player?.status)

        switch player!.status {

            case AVPlayerStatus.Failed:
                print("Player item status failed")
                player?.play()
            break

            case AVPlayerStatus.ReadyToPlay:
                print("Player item status is ready to play")
                activityView.stopAnimating()
            break

            case AVPlayerStatus.Unknown:
                print("player item status is unknown")
            break
        }

        switch keyPath! {

            case "playbackBufferFull":
                activityView.stopAnimating()
                print("playbackBufferFull")
            break

            case "playbackLikelyToKeepUp":
                activityView.stopAnimating()
                print("playbackLikelyToKeepUp")
            break

            case "playbackBufferEmpty":
                activityView.startAnimating()
                print("playbackBufferEmpty")
            break

            case "loadedTimeRanges":
                print("loadedTimeRanges")

            default:
                print("Error")
            break
        }
    }
}

}

  

输出

AVPlayer在此行Player item status is ready to play

之后(5,6,7秒)开始播放
-1
1
Optional(__C.AVPlayerStatus)
Player item status is ready to play
Error
1
-1
-1
1
Optional(__C.AVPlayerStatus)
Player item status is ready to play
Error

enter image description here

2 个答案:

答案 0 :(得分:0)

在您的代码中,每当按下一个单元格时,您都有activityView.stopAnimating()。所以无论如何,动画都将开始或停止点击,具体取决于if sender.selected == false

所以我想如果你删除activityView.stopAnimating()那么它就不会那么早停止动画。由于我不确定输出的确切位置,因此很难阅读您的问题和代码。

答案 1 :(得分:0)

关于第二个问题,即“并且它再次启动时再次启动activityIndi​​cator(加载,缓冲)”

我认为解决方法可能是以下之一: -

a)

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.item)!")
//Start activityView Animation when user clicks on any item which won't be buffered by default
activityView.startAnimating()
}

b)

var selectIndex:Int = -1 {
didSet {
      //Start activityView Animation when user clicks on any item which won't be buffered by default
      activityView.startAnimating()
       }
}