D
D
Dima Grib2019-01-29 15:31:23
go
Dima Grib, 2019-01-29 15:31:23

Playing the player in only one cell?

Friends, tell me, I'm looking for a solution, how to play audio / video in only one cell. Since there can be 10-20 different videos/music in a section, the user can run the play function everywhere. Interested in how to implement such solutions

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videCellId, for: indexPath) as! InsideVideoCell
    cell.playButton.tag = indexPath.row // 05.01
    cell.playButton.addTarget(self, action: #selector(playpause), for: .touchUpInside) // 05.01
    if let videosArray = videos?[indexPath.item] {
        queueUtility.async {
            let url = URL(string: videosArray)
            let playerItem = AVPlayerItem(url: url!)
            DispatchQueue.main.async {
                self.player = AVPlayer(playerItem: playerItem)
                self.player.actionAtItemEnd = .none
                let videoPlayerLayer = AVPlayerLayer(player: self.player)
                videoPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
                videoPlayerLayer.frame = cell.bounds
                cell.insertSubview(cell.videoPlayerController.view, at: 0)
                cell.contentView.addSubview(cell.playButton)
                cell.videoPlayerController.player = self.player
                cell.videoPlayerController.view.frame = videoPlayerLayer.frame
            }
        }
    } else {
        print("Нет Видео")
    }
    return cell
}

I use sender.tag - I start/stop the video in each cell, but I'm interested in a method how to make it possible to play only in one cell
Thank you

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Semchenko, 2016-06-12
@valerafedorchenko

Have you tried reading the documentation?
----------------------------------------------------
https://golang .org/pkg/encoding/json/
https://blog.golang.org/json-and-go
https://gobyexample.com/json

O
one pavel, 2019-01-29
@onepavel

Player initialization code has no place in the cell creation method.
Create a separate object that controls the player and pass commands to it
from after clicking on the item, I think in didSelectRowAt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question