YouTube video playback broken on iOS6 (works fine on iOS5)

前端 未结 8 2184
南旧
南旧 2020-12-13 16:13

In my app, I have a button which, when pressed, lets you watch a youtube video (a movie trailer). Within the app, without launching safari. Below you can see a code snippet.

相关标签:
8条回答
  • 2020-12-13 16:28

    Untested and not sure if that is the problem here, becasue I don't know the URL you're using. But I stumbled right about following:

    As of iOS 6, embedded YouTube URLs in the form of http://www.youtube.com/watch?v=oHg5SJYRHA0 will no longer work. These URLs are for viewing the video on the YouTube site, not for embedding in web pages. Instead, the format that should be used is described here: https://developers.google.com/youtube/player_parameters.

    from http://thetecherra.com/2012/09/12/ios-6-gm-released-full-changelog-inside/

    0 讨论(0)
  • 2020-12-13 16:29

    Apple changed how YouTube videos are handled in iOS6. I also was using the findButtonInView method but that no longer works.

    I've discovered the following seems to work (untested in iOS < 6):

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        self.webView.mediaPlaybackRequiresUserAction = NO;
    }
    
    - (void)playVideo
    {
        self.autoPlay = YES;
        // Replace @"y8Kyi0WNg40" with your YouTube video id
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://www.youtube.com/embed/", @"y8Kyi0WNg40"]]]];
    }
    
    // UIWebView delegate
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        if (self.autoPlay) {
            self.autoPlay = NO;
            [self clickVideo];
        }
    }
    
    - (void)clickVideo {
        [self.webView stringByEvaluatingJavaScriptFromString:@"\
            function pollToPlay() {\
                var vph5 = document.getElementById(\"video-player\");\
                if (vph5) {\
                    vph5.playVideo();\
                } else {\
                    setTimeout(pollToPlay, 100);\
                }\
            }\
            pollToPlay();\
         "];
    }
    
    0 讨论(0)
  • 2020-12-13 16:34

    dharmabruce solution works great on iOS 6, but in order to make it work on iOS 5.1, I had to substitute the javascript click() with playVideo(), and I had to set UIWebView's mediaPlaybackRequiresUserAction to NO

    Here's the modified code:

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        self.webView.mediaPlaybackRequiresUserAction = NO;
    }
    
    - (void)playVideo
    {
        self.autoPlay = YES;
        // Replace @"y8Kyi0WNg40" with your YouTube video id
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://www.youtube.com/embed/", @"y8Kyi0WNg40"]]]];
    }
    
    // UIWebView delegate
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        if (self.autoPlay) {
            self.autoPlay = NO;
            [self clickVideo];
        }
    }
    
    - (void)clickVideo {
        [self.webView stringByEvaluatingJavaScriptFromString:@"\
            function pollToPlay() {\
                var vph5 = document.getElementById(\"video-player-html5\");\
                if (vph5) {\
                    vph5.playVideo();\
                } else {\
                    setTimeout(pollToPlay, 100);\
                }\
            }\
            pollToPlay();\
         "];
    }
    
    0 讨论(0)
  • 2020-12-13 16:39

    I have solved the problems with dharmabruce and JimmY2K. solutions, with the fact that it only works the first time a video is played, as mentioned by Dee

    Here is the code(including event when a video ends):

    - (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
        videoView = [[UIWebView alloc] init];
        videoView.frame = frame;
        videoView.delegate = self;
    
        [videoView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];// this format: http://www.youtube.com/embed/xxxxxx
        [self.view addSubview:videoView];
    
        //https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MediaPlayer.framework/MPAVController.h
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playbackDidEnd:)
                                                     name:@"MPAVControllerItemPlaybackDidEndNotification"//@"MPAVControllerPlaybackStateChangedNotification"
                                                   object:nil];
    
    }
    
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        //http://stackoverflow.com/a/12504918/860488
        [videoView stringByEvaluatingJavaScriptFromString:@"\
                                        var intervalId = setInterval(function() { \
                                            var vph5 = document.getElementById(\"video-player\");\
                                            if (vph5) {\
                                                vph5.playVideo();\
                                                clearInterval(intervalId);\
                                            } \
                                        }, 100);"];
    }
    
    - (void)playbackDidEnd:(NSNotification *)note
    {
        [videoView removeFromSuperview];
        videoView.delegate = nil;
        videoView = nil;
    }
    
    0 讨论(0)
  • 2020-12-13 16:43

    I've found that the code below works for both iOS5 & iOS6. In my example, I have a text field that contains the url to the video. I first convert the formats to the same starting string 'http://m...". Then I check to see if the the format is the old format with the 'watch?v=' and then replace it with the new format. I've tested this on an iPhone with iOS5 and in the simulators in iOS5 & iOS6:

    -(IBAction)loadVideoAction {
        [activityIndicator startAnimating];
    
        NSString *urlString = textFieldView.text;
        urlString = [urlString stringByReplacingOccurrencesOfString:@"http://www.youtube" withString:@"http://m.youtube"];
        urlString = [urlString stringByReplacingOccurrencesOfString:@"http://m.youtube.com/watch?v=" withString:@""];
        urlString = [NSString stringWithFormat:@"%@%@", @"http://www.youtube.com/embed/", urlString];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [activityIndicator stopAnimating];
    }
    
    0 讨论(0)
  • 2020-12-13 16:47

    I believe the problem is that when webViewDidFinishLoad was triggered, the UIButton was not added to the view yet. I implemented a small delay to find the button after the callback is returned and it works for me on ios5 and ios6. drawback is that there is no guarantee the UIButton is ready to be found after the delay.

    - (void)autoplay {
        UIButton *b = [self findButtonInView:webView];
        [b sendActionsForControlEvents:UIControlEventTouchUpInside]; }
    
    - (void)webViewDidFinishLoad:(UIWebView *)_webView {
        [self performSelector:@selector(autoplay) withObject:nil afterDelay:0.3]; }
    

    I had to use the url http://www.youtube.com/watch?v=videoid this is the only way it will work for me

    0 讨论(0)
提交回复
热议问题