在集合视图中以单个单元格播放视频

时间:2017-03-15 12:38:31

标签: ios iphone swift uicollectionview avplayer

我在单个单元格中的集合视图单元格中播放视频时遇到问题

单元可以同时拥有视频和图像,当视频下载并从本地目录播放视频时

集合视图每行有2个单元格

问题是:我希望视频一次只能在1个单元格中播放,并在视频不再可见时停止播放

我在单元格中使用以下代码作为索引路径中的项目。

let localVideoURL = getDownloadedVideoPathFromDocumentDirectory(filename: eachTimeLineFeed.mediaFile!)
    print("Play Video from URL \(localVideoURL)")

        let item = AVPlayerItem(url: localVideoURL!);
        self.avPlayer = AVPlayer(playerItem: item);
        self.avPlayer?.actionAtItemEnd = .none
        self.avPlayerLayer = AVPlayerLayer(player: self.avPlayer);

        self.avPlayerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.avPlayerLayer?.frame = CGRect(x: 0, y: 0, width: SCREENWIDTH()/2, height: SCREENWIDTH()/2)
        cell.viewVideo.layer.addSublayer(self.avPlayerLayer!)
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd), name: NOTIFICATION_PLAYER_DID_END_PLAYING_VIDEO, object: self.avPlayer?.currentItem!)
        self.avPlayer?.play()

1 个答案:

答案 0 :(得分:2)

将collectionview可选委托添加到viewcontroller collectionView:didEndDisplayingCell:forItemAtIndexPath:,在此函数中,您可以访问indexpath,cell和collection视图。然后你可以停止在单元格项目上播放视频。

斯威夫特

from kivyoav.delayed import delayable

@delayable
def Diplay_Label_when_waiting(self, *args):
    self.add_widget(Label_when_waiting)
    yield 0.0 # delay of 0ms , will cause the UI to update...
    DisplayTable(self, *args)

目标-C

func collectionView(_ collectionView: UICollectionView,
                    didEndDisplaying cell: UICollectionViewCell,
                    forItemAt indexPath: IndexPath) {

}