0

我正在尝试在我的应用中集成一个 youtube 视频。但我希望它在视频完成后恢复到调用 viewController。我几乎成功地实现了这一点,但以我的方式,用户必须按下完成按钮,然后再按下一个按钮才能返回原始 ViewController。

这是我使用的代码,请帮助,我更喜​​欢参考完整代码或代码示例。

YouTubeView.h:

#import <UIKit/UIKit.h>

@interface YouTubeView : UIWebView
{
}

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType;
@end

YouTubeView.m:

#import "YouTubeView.h"

@interface YouTubeView ()

@end

@implementation YouTubeView


- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType
{
NSString *strMimeType;

if([mimeType length]>0)
{
    strMimeType = mimeType;
}

else
{
    strMimeType =@"x-shockwave-flash"; //@"x-shockwave-mp4";
}

if (self = [super init])
{
    // Create webview with requested frame size
    self = [[UIWebView alloc] initWithFrame:frame];

    // HTML to embed YouTube video

    NSString *videoHTML = [NSString stringWithFormat:@"\
                 <html>\
                 <head>\
                 <style type=\"text/css\">\
                 iframe {position:absolute; top:50%%; margin-top:-130px;}\
                 body {background-color:#000; margin:0;}\
                 </style>\
                 </head>\
                 <body>\
                 <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
                 </body>\
                 </html>", urlString];

    [self loadHTMLString:videoHTML baseURL:nil];

}

return self;

}


- (void)dealloc
{
[super dealloc];
}

@end

还有一个指向 *on 主 ViewController 的按钮:-(IBAction)runTestVideo:(id)sender {

NSString *strNormalVdoURL = [NSString stringWithFormat:@"http://www.youtube.com/embed/TgLqH9n57B0"];

YouTubeView *videoVw = [[YouTubeView alloc] initWithStringAsURL:[NSString stringWithFormat:@"%@",strNormalVdoURL] frame:CGRectMake(0,0,315,420) mimeSubType:@"x-shockwave-flash"];

                        [self.view addSubview:videoVw];
                        [videoVw release];
                        videoVw = nil;

}

请帮忙。

4

1 回答 1

0

我不太确定我的方法是否适用于你的方法,因为我在播放视频时隐藏了视图。用户按下完成按钮后,电影控制器将退出并显示视图。

- (void)viewDidLoad { 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (MPMoviePlayerDidExitFullScreen;) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
}

- (void)MPMoviePlayerDidExitFullScreen:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name: MPMoviePlayerDidExitFullscreenNotification object:nil];

[movieController stop];
[movieController.view removeFromSuperview];

view.hidden = NO;
}
于 2012-07-19T02:45:50.913 回答