ios - AVPlayerLayer slow instantiation -
note : accept answer in xamarin.ios, objective-c or swift. if need reading xamarin code can clear you, should fine considering "complexity" of code here.
i suspect avplayerlayer
slow down process.
i'm embedding video/audio player in controller of mine, using following code in viewdidload
:
void setuplayer() { _layer = avplayerlayer.fromplayer(_player); _layer.frame = playerviewcontainer.bounds; _layer.player = _player; _layer.backgroundcolor = uicolor.black.cgcolor; _layer.videogravity = avlayervideogravity.resizeaspect; //this uiview playerviewcontainer.layer.insertsublayer(_layer, 0); }
as this, in constructor of own controller (containing player) :
public void seturls(string mediaurl, string coverurl = null) { coverurl = string.isnullorempty(coverurl) ? string.empty : coverurl; mediaurl = mediaurl; avurlasset asset = new avurlasset(new nsurl(mediaurl)); avplayeritem item = new avplayeritem(asset); _isvideo = item.asset.tracks.length != 1; _player = new avplayer(item); }
this relevant code called in viewdidload
, constructor. parts removed should of no influence whatsoever (settings texts, loading assets , whatnot).
now issue i'm facing loading "big" (by big, don't mean more 4 hour video stream. 4 minutes long. ) file takes lot of time. 4 seconds. between 0.5-1.5 in constructor (seturls
function), , between 1.5-2.5 in viewdidload
(setuplayer
function).
i've timed it, , removing methods drastically reduces lag decent (but have no image or bad results).
i've compared native player, using following code :
_item = avplayeritem.fromurl(url); _player = new avplayer(_item); _controller = new avplayerviewcontroller(); _controller.player = _player; this.presentviewcontroller(_controller, true, _player.play);
and loads instantly. it's not 4 seconds of loading more evenly spread, it's takes less 4 seconds altogether.
considering it's loading during animation, , screen still black , not playing when player displayed, i'd native controller job in 1-2 seconds (while half of during animation).
mine before animating/showing anything. nice thing video plays can, no blackscreen. downside horrible loading time of controller.
note : during loading time, ui blocked. leads me believe it's happening on ui thread , that, maybe, shouldn't.
my questions :
- where bottlenecks here ? i'm doing wrong.
- what should differently? maybe way i'm loading player in own controller ?
- how can load components asynchronously avoid blocking ui thread? enough reduce loading time ? or simply, allowed it?
convert method async , call await asset.loadvaluestaskasync(new string[] { @"playable" });
after creating asset. preload video not block ui.
Comments
Post a Comment