How to Open Youtube video in MPMoviePlayerControll

2019-07-02 06:17发布

i use this method before 1 time. but Now i can not get video from this URL that direct-play YouTube video in MPMoviePlayerController.

URL = 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];

}

and My movieFinishedCallback is as Under ...

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

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-02 06:32

if possible to open your URL into UIWebview then take a button in your class and redirect it on to the WebsiteViewController class may be it helps u

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]; 
}
查看更多
别忘想泡老子
3楼-- · 2019-07-02 06:44

Create a custom uiwebview: please follow instruction

1) Create a interface .h file named "YouTubeView.h"

#import <UIKit/UIKit.h>

@interface YouTubeView : UIWebView 
{
}

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

@end

2) in to .m file

 #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) Import the youtubeview file in the controller where you want to play you tube video and create uiview like the code below

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;

Note: Video will play in device only so check in device

查看更多
登录 后发表回答