I 1周时间之前使用此方法。 但现在我不能从这个URL中的MPMoviePlayerController即直接播放YouTube视频获得视频。
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];
}
和我movieFinishedCallback
是在...
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}
如果可能的话,打开您的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];
}
创建自定义的UIWebView:请按照指示
1)创建.h文件名为“YouTubeView.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;
注:视频会在设备上播放只所以在设备检查