Swift - AVPlayer - Stop player in another cell when a new player starts

I am working on a podcast application and achieved all the streaming-related parts of the application. The problem explained below, how can I achieve this?

Current:
The player view is hidden by default. When the user presses the "Play" button, the player view unhides and the player starts streaming. But it won't stop streaming if the user presses another "Play" button so both streams play simultaneously.

Goal:
When the user starts playing a new podcast by pressing another Play button, I want other players would stop playing and disappear.

Extra:
The whole podcast view awakes from the xib cell. So, the problem is, how can I manipulate another cell view's member when pressing a button in another cell view
The error on line 93 gone and I compiled the code successfully. This is the output:

Code Block debugger
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found


Stop when another start function still not working for information.
It is normal at first pass, because no player yet defined.

Could you add another print:
Code Block
if url != "" {
let playerItem = AVPlayerItem( url:NSURL( string:url )! as URL )
print("New player for", url)
player = AVPlayer(playerItem:playerItem)
player!.rate = 1.0;


This is the output with the latest print:

First click (to show/play)

Code Block debugger
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player found
cell is PodcastViewCell
podCell.player NOT found
cell is NOT PodcastViewCell
cell is PodcastViewCell
podCell.player found
cell is PodcastViewCell
podCell.player NOT found
New player for https://podcast.link


Second click on same button (to hide/stop):

Code Block debugger
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player found
cell is PodcastViewCell
podCell.player NOT found
cell is NOT PodcastViewCell
cell is PodcastViewCell
podCell.player found
cell is PodcastViewCell
podCell.player NOT found

OK, I missed it, you have effectively some cell with active player.

It is surprising in the first run that you have 2 found, as none has yet been created

What are exactly the cells you hit ?

Could you add yet another print:
Code Block
@IBAction func playPodcast(_ sender: Any) {
print("Title", self.title)
if let collectionView = self.superview as? UICollectionView {
let visibleCells = collectionView.visibleCells


There is also a case
cell is NOT PodcastViewCell
What cell is this ?

Sorry for the late reply. How are you lately? I hope everything going great!

I tried to add the latest print but got an error.

Code Block swift
print("Title", self.title) <----------------- "Value of type 'PodcastViewCell' has no member 'title'"



It is surprising in the first run that you have 2 found, as none has yet been created

I pressed different play buttons while other players are active, so this should why there are "player found" outputs shown.


There is also a case cell is NOT PodcastViewCell What cell is this ?

I rerun the code with prints and didn't get any NOT values. I will debug and notify you when I get NOT values. This is the output I got:

Code Block debugger
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found
cell is PodcastViewCell
podCell.player NOT found


I did not read your code with enough attention.
It is not title but titleLabel.
Thank you for all of your efforts but I am really giving up on this idea. Instead, I'll try to accomplish it in another way. When the user clicks the "play button", I'll show "podcast view" with a blurry background and a player. When the user wants to close, I'll stop the player and then hide the view. I am now looking into how can I create on PodcastViewCell file but show a full-screen view out of the fixed-size cell.

I am open to advice but I'd totally understand if you say "meh that's all from me" 😇
To go further would require to get the whole code to undertsand what happens exactly by running code.

So, continue with your other idea (I responded to the thread).

And don't forget to close this thread by marking the correct (or most useful) answer.

Try both at same time:

               "yourAVPlayer".replaceCurrentItem(with: nil)
              "theURLoftheAVPlayer".removeAll()
Swift - AVPlayer - Stop player in another cell when a new player starts
 
 
Q