ios - AVPlayerLayer isn't showing AVPlayer video? -
what's trick getting avplayer video content show in one's view?
we using following avplayer code nothing appearing on screen. know video there because able show using mpmovieplayercontroller.
here code using:
avasset *asset = [avasset assetwithurl:videotempurl]; avplayeritem *item = [[avplayeritem alloc] initwithasset:asset]; avplayer *player = [[avplayer alloc] initwithplayeritem:item]; player.actionatitemend = avplayeractionatitemendnone; avplayerlayer *layer = [avplayerlayer playerlayerwithplayer:player]; // layer.frame = self.view.frame; [self.view.layer addsublayer:layer]; layer.backgroundcolor = [uicolor clearcolor].cgcolor; //layer.backgroundcolor = [uicolor greencolor].cgcolor; [layer setvideogravity:avlayervideogravityresizeaspectfill]; [player play];
are setting layer improperly current view?
you need set layer's frame property. e.g.:
self.playerlayer.frame = cgrectmake(0, 0, 100, 100)
if tried , didn't work in view controller's view, tried set layer's frame
property view-controllers frame
or bounds
property {0, 0, 0, 0}
@ time avplayerlayer
created. need set frame of player during layout-passes, @ point view-controller's frame
set other {0, 0, 0, 0}
. properly:
if you're using auto-layout in custom uiview (including ib):
override func layoutsubviews() { super.layoutsubviews() //match size of view catransaction.begin() catransaction.setdisableactions(true) self.playerlayer.frame = self.bounds catransaction.commit() }
if you're using auto-layout in custom uiviewcontroller:
override fun viewdidlayoutsubviews() { //match size of view-controller catransaction.begin() catransaction.setdisableactions(true) self.playerlayer.frame = self.view.bounds catransaction.commit() }
the catransaction
lines disable implicit animations on layer's frame change. if you're wondering why not needed, it's because layers uiview's not implicitly animate default. in case, using non-view backed layer (avplayerlayer
)
the best route take add new view view-controller through interface build , set custom class on newly added view. create custom view class , implement layoutsubviews
code.
Comments
Post a Comment