2

I'm using an Objective-C project for reference for a very similar Swift app. Basically, videos are uploaded to Firebase and the download URL is saved to a "Post" object and played in a UITableViewCell upon retrieval. In the Objective-C project (also using Firebase) it works perfectly but I'm having some trouble in Swift.

Objective-C Code, inside cell, like I said, works perfectly.

- (void)playVideoWithURL:(NSURL *)url {

    self.videoPlayer = [[AVPlayer alloc]initWithURL:url];
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
    layer.frame = self.videoView.bounds;
    [layer setVideoGravity:AVLayerVideoGravityResizeAspect];
    [self.videoView.layer addSublayer:layer];

    [self.videoPlayer play]; 

}

Swift Code, also inside cell, doesn't throw any errors or crash, compiler says URL exists as well as the player and layer (whose frame is not CGRectZero, checked that too).

@IBOutlet var usernameLabel: UILabel!
@IBOutlet var captionLabel: UILabel!
@IBOutlet var avPlayerView: UIView!

var videoPlayer : AVPlayer!

func playVideoWithURL(url: NSURL) {

    self.videoPlayer = AVPlayer(URL: url)

    let layer = AVPlayerLayer(player: self.videoPlayer)
    layer.frame = avPlayerView.bounds
    layer.videoGravity = AVLayerVideoGravityResizeAspect
    avPlayerView.layer.addSublayer(layer)

    videoPlayer.play()

}

To keep it simple I won't post any Firebase code just yet since it all works in the Objective-C project and the download urls are successfully retrieved in the Swift one as well, leading me to believe it has nothing to do with the issue.

Any ideas? This is driving me crazy. Thank you!

UPDATE:

AVPlayer is not actually the issue, since it works with a test video. The issue is with Firebase, currently investigating further.

Echizzle
  • 3,359
  • 5
  • 17
  • 28

1 Answers1

-1

Try initializing your player with a player item.

let playerItem = AVPlayerItem(URL: url)
self.videoPlayer = AVPlayer(playerItem: playerItem)

Also, you should check the status of the player to determine exactly when it is ready to play using KVO.

See Knowing when AVPlayer object is ready to play.

The error is available in playerItem.error? if the player has a status of failed. Check that to help determine the problem.

Daniel Zhang
  • 5,778
  • 2
  • 23
  • 28
  • Nah, still nothing. I initially thought the same thing but I've initialized AVPlayer without an item and just URL before so it has to be something else... thanks though – Echizzle Aug 13 '16 at 02:23
  • 1
    I added some additional things to try to my answer to help you debug the problem. You should report the player status and any errors that you are getting. – Daniel Zhang Aug 13 '16 at 15:02
  • Thanks, the error is nil and the item exists, showing the Firebase download URL, just like the other project where it works. I'll look into the KVO though. I just want to know why the Objective-C code works so perfectly yet the same Swift code doesn't. I know both languages work a little differently but still! – Echizzle Aug 13 '16 at 16:30
  • Big breakthrough, I used a test URL and it works fine so AVPlayer is not the issue. I did, however, discover that while the URL returned from firebase looks identical when logged to the console, the Objective-C one contains a bunch of properties in the debugger, one called _clients with the same url casted to a NSCFString while the Swift version just shows the URL. Also the Objective-C project's videos uploaded as "Video/Quicktime" while the Swift project's uploaded just as "Videos" in the type column of storage. I'll an image of the console above. – Echizzle Aug 13 '16 at 19:38