Reputation: 380
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.
Upvotes: 4
Views: 1326
Reputation: 380
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.
Upvotes: 3