AVPlayerが音楽の再生を開始するときにactivityIndicatorを停止しようとしていますが、もう一度AVPlayerを起動(ロード、バッファリング)するとactivityIndicatorを開始します。それは少し働いて、問題は、AVPlayerは音楽を再生する前に数秒(5,6,7)前にactivityIndicatorを停止することです。また、もう一度activityIndicatorを起動しないでください(ロード、バッファリング)。いずれにせよ、私の間違いはどこにあるのか、私はそれを修正する必要があるのかは分かりません。おかげAVPlayerは再生前にUIActivityIndicatorを停止します
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(5,6,7秒)の後に、このラインの再生を開始Player item status is ready to play
-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
私が最初にplayボタンを押したとき、そのコードは 'audioControlButtonAction'ボタンの' if sender.selected == false {............} 'を実行しますか? – ZAFAR007