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

前端 未结 8 2188
南旧
南旧 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: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;
    }
    

提交回复
热议问题