如何在MPMoviePlayerController中打开Youtube视频并播放它

时间:2012-06-25 10:28:33

标签: objective-c ios4 mpmovieplayercontroller

我在1次之前使用此方法。但现在我无法从此URL获取直接播放MPMoviePlayerController中的YouTube视频的视频。

网址= http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded

- (void)viewDidLoad
{

   player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded"]]];

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(movieFinishedCallback:)
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

//---play movie---
[player prepareToPlay];
[player pause];

player.view.frame = CGRectMake(0, 0, 320, 367);

[self.view addSubview:player.view];   

[super viewDidLoad];

}

和我的movieFinishedCallback为Under ...

- (void) movieFinishedCallback:(NSNotification*) aNotification 
{ 
 player = [aNotification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];
}

2 个答案:

答案 0 :(得分:2)

创建自定义uiwebview:请按照说明

1)创建名为“YouTubeView.h”的界面.h文件

#import <UIKit/UIKit.h>

@interface YouTubeView : UIWebView 
{
}

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

@end

2)进入.m文件

 #import "YouTubeView.h"

@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 *youTubeVideoHTML = @"<html><head>\
        <body style=\"margin:0\">\
        <embed id=\"yt\" src=\"%@\" type=\"application/%@\" \
        width=\"%0.0f\" height=\"%0.0f\"></embed>\
        </body></html>";

        // Populate HTML with the URL and requested frame size
        NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString,strMimeType, frame.size.width, frame.size.height];

        // Load the html into the webview
        [self loadHTMLString:html baseURL:nil];
    }

    return self;  

    }


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

    @end

3)在你想播放管视频的控制器中导入youtubeview文件并创建如下代码的uiview

YouTubeView *videoVw = [[YouTubeView alloc] initWithStringAsURL:[NSString 

stringWithFormat:@"%@",strNormalVdoURL] frame:CGRectMake(0,0,320,480) mimeSubType:@"x-shockwave-

flash"];

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

注意:视频仅在设备中播放,因此请检入设备

答案 1 :(得分:1)

如果可能的话,将URL打开到UIWebview然后在您的课程中按一个按钮并将其重定向到WebsiteViewController课程可能会对您有所帮助

WebsiteViewController.h /// ----

 @interface WebsiteViewController : UIViewController <UIWebViewDelegate>
 {
  IBOutlet UIWebView *webView;
 }

 @property(nonatomic,retain) IBOutlet UIWebView *webView;

WebsiteViewController.m /// ---

-(void)viewDidLoad {
 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:@"url"];
 [self.webView setScalesPageToFit:YES];
 [self.webView loadRequest:request];
 [request release]; 
}