1

I am trying to add an AVPlayerViewController to a chat bubble so that any audio or video messages when received can be directly played in the chat bubble. My UITableView list contains many of these bubbles. But when I try to scroll the table view, the app gets crashed showing -[AVPlayerViewController retain]: message sent to deallocated instance.

Here below is my code for adding a AVPlayerViewController object to each chat bubble :-

_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:url];
_playerViewController.view.frame = CGRectMake(10.0, 5.0, 190.0 , 90.0);  //   self.view.bounds;
_playerViewController.showsPlaybackControls = YES;

[_playerViewController.view setTranslatesAutoresizingMaskIntoConstraints:YES];
[_playerViewController .view setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
[cell.contentview addsubview:_playerViewController.view];

the url contains the file path (audio/video), and I am trying to add the _playerViewController object to UITableView cell's content view, but it throws the above error. Can anyone please tell me what I should be doing.

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41

1 Answers1

1

Every cell should not contain AVPlayerViewController. It's horrible!

You should manage your stuff like every cell just display thumbnail of your video and you should manage array of urls of videos. now use common AVPlayerViewController to play any video. I mean when user tap cell or play button of cell then pass that url to player and play video!!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Thanks for the suggestion. Can you also tell me the logic for playing audio files please. – Amuuu Gaikwad Oct 21 '16 at 10:44
  • @Lion-should i use the same player too play the audio files? – Amuuu Gaikwad Oct 21 '16 at 10:45
  • you can create a action method that create and plays video (similar code as you have shown in question). then on button click or in `didselectrow` pass selected video url to these method and create AVPlayerViewController and play video! – Ketan Parmar Oct 21 '16 at 10:50