Search code examples
iosobjective-cyoutubeyoutube-apiytplayerview

YouTube iframe is too slow in iOS app


I am using YouTube API in my iOS application and the problem is that it loads really slow on my iPhone, but on iOS simulator it works perfectly.

I tried it on different networks and on different iPhones and its always the same. On every network simulator works a lot better.

I'm using YTPlayerView-iframe-player.html and YTPlayerView.

So, the question is can I do anything to load it faster or can I start playing song after it loads?


Solution

  • There is no way to speed up your network. You can do two things to improve the user experience: hide the player until the video loads, and start playing the video as soon as possible (autoplay).

    Add the autoplay key to your playervars dictionary:

    NSDictionary *playerVars = @{
                                 @"playsinline" : @1,
                                 @"autoplay" : @1, // <- here
                                 @"showinfo" : @0,
                                 @"rel" : @0,
                                 @"modestbranding" : @1,
                                 };
    
    [self.playerView loadWithVideoId:videoId playerVars:playerVars];
    
    // hide the player view until it is ready
    self.playerView.hidden = YES;
    

    Unhide the player when ready:

    // unhide and play
    - (void)playerViewDidBecomeReady:(YTPlayerView *)playerView { 
        self.playerView.hidden = NO;
    }
    

    or

    - (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state {
        if (state == kYTPlayerStatePlaying) {
            self.playerView.hidden = NO;
        }
    }