4

The views init and create everything just fine, however when I move the AVPlayer from one view layer to another, it disappears. The video goes away (black on full screen view and white on original sized view), but the audio still plays. My controls still work (play/pause/scrub).

My application was built for iOS7. In the simulator and on devices, this works. In the simulator for iOS8, this works. Only on iOS8 devices does this not work.

Code below:

AVAsset *avAsset = [AVAsset assetWithURL:fileURL];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.currentPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];
AVPlayerLayer *tmpPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.currentPlayer];
tmpPlayerLayer.frame = self.videoDetailViewPlayer.bounds;
[self.videoDetailViewPlayer.layer addSublayer:tmpPlayerLayer];

*fileURL is an NSURL properly formatted and retrieves a proper object with a fully qualified URL tested against each OS. **self.videoDetailViewPlayer.bounds is full screen and I've checked to make sure it does have proper w & h. (I have tried [tmpPlayer setFrame] as well as the current setup above, using both frame and bounds. In everything but iOS8 on a device, it works.

Again, audio continues, but the video seems lost.

Edit: I am not sure what causes the player to continue working, but the AVPlayer will keep playing audio, regardless of whether there is a layer for video. I have 2 views that I am alternating the playerlayer between, and maybe I should just add it to both, but according to the docs, "Only the most recently created player layer will actually display the video content on-screen." (https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayerLayer_Class/index.html). If I create the tmpPlayerLayer from the original view, add that to my other view, then make the layer nil for the first view, it works (but won't resize, yet). If I try to create a new AVPlayerLayer (as noted above), it just stops displaying video completely...still investigating, but any pointers might help.

lordB8r
  • 370
  • 2
  • 12

1 Answers1

3

Instead of creating a new AVPLayerLayer for each view, I found that creating a playerlayer for the controller, and reusing that between the views, allows me to switch between the UIViews (full and normal). I don't know why iOS8 native breaks this functionality, but this workaround means that the last PlayerLayer created still displays the video, and whichever view I add it to, it plays there.

lordB8r
  • 370
  • 2
  • 12
  • I can get behind this reasoning; it seems like the direction the docs are pointing you in - one layer per `AVPlayer`. Just a thought, but is it possible that you can reuse the `UIView`? I had a similar-ish desire to animate from framed to full screen [here](http://stackoverflow.com/questions/8918105/animate-avplayerlayer-videogravity-property) – edelaney05 Oct 22 '14 at 15:35
  • I don't think the docs point me in that direction. "You can create arbitrary numbers of player layers with the same AVPlayer object." The previous code (before my solution) worked in the simulator, but not on the device, so it seems that the creation of the new AVPlayerLayer was not displaying video like it should have been. If this is reproducible inside of multiple views, then this might be a bug and not behaving as expected – lordB8r Oct 23 '14 at 16:29
  • Docs say: "You can create many AVPlayerLayer objects from a single AVPlayer instance, but only the most recently created such layer will display any video content onscreen." and then continue on to point out using two `AVPlayer`s to play the same asset at the same time. I read this as player layers aren't the right abstraction level to manage multi-view playback. It is worth filing a bug against both documentation and `AVPlayer` with a cross reference. – edelaney05 Oct 23 '14 at 18:11
  • This is a bug. Sent it to Apple, they closed mine in favor of the one already in place (18301836) – lordB8r Nov 03 '14 at 20:24
  • @lordB8r How do you resuse an AVPlayerLayer for multiple views? – KarenAnne Oct 29 '15 at 08:31