Youtube - iPhone - displaying UIWebView of video

走远了吗. 提交于 2019-12-18 04:25:14

问题


I have a script in my app that loads a Youtube link in a section of the UIView, the link is provided by my server (mySQL/using php) and from that it is loaded... this script below works fine..... however...

Here is what I have right now, I was looking to remove the programmatically part (where it gives the dimensions and possibly replace it with a UIWebView) to it, so I could improvise using IB, this will help when my application launches, a quick animation occurs and then I want this youtube link to load and display after viewDidLoad is completed.

.h file:

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;

.m file

//view did load function...

[self embedYouTube:youtubestring frame:CGRectMake(69,72,184,138)];

//after the view did load is closed I have..

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}

any help would greatly be appreciated!! thanks


回答1:


This works using UIWebViews created in IB:

The .h File:

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

-(void)embedYouTubeInWebView:(NSString*)url theWebView: (UIWebView *)aWebView;

The .m File:

in the viewDidLoad:

[self embedYouTubeInWebView:youTubeString theWebView:infoVideo1];

the Method:

- (void)embedYouTubeInWebView:(NSString*)url theWebView:(UIWebView *)aWebView {     

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

NSString* html = [NSString stringWithFormat:embedHTML, url, aWebView.frame.size.width, aWebView.frame.size.height]; 

[aWebView loadHTMLString:html baseURL:nil];
}

You can even have multiple webViews on a single screen. Hope this helps.




回答2:


Aehm, the iPhone doesn't have Flash. It never had and probably never will have. The only way to open youtube videos on the iPhone is via the youtube app, which plays them back in another format. An example for this is [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com /v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM"]];
You can find more detail here: http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html




回答3:


iOS has a safari plugin which plays youtube videos if they embedded as flash.

This uiwebview category I've put together will embed videos for you without haven't to leave the application or a tutorial :-)

https://github.com/enigmaticflare/UIWebView-YouTube--iOS-Category--ARC-compliant-code



来源:https://stackoverflow.com/questions/4729538/youtube-iphone-displaying-uiwebview-of-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!