3

我在 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的一样在...

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

2 回答 2

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;

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

于 2012-06-25T11:31:21.417 回答
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]; 
}
于 2012-06-25T10:43:20.763 回答