开发者

Play Youtube video in iPhone

I want to run a vide from youtube into my iPhone simulator and the URL of the vide is this

http://www.youtube.com/v/zL0CCsdS1cE

but i am not able to load this video into my iPhone, i am using a webview to display this video and here's the code to do that

objwebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:tempString]];
[objwebView loadRequest:request];

based upon the answer i tried the below code and here's a new version of my code

mywebView = [[UIWebView alloc]initWithFrame:self.view.bounds];

NSString *youTubeURL = @"http://www.youtube.com/v/zL0CCsdS1cE";

NSString *htmlString = [NSString stringWithFormat:@"<body style=\"margin:0;background-color:#222222;\"><div style=\"padding:%i;width:%i;height:%i;background-color:#ffffff;\"><video width=\"%i\" height=\"%i\" controls=\"\" autoplay=\"\" tabindex=\"0\"><source src=\"%@\"></source></video></div></body>",10, (int)(self.view.bounds.size.width), (int)(self.view.bounds.size.height), 320, 480,youTubeURL];

[mywebView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:mywebView];

But still i cant get the video t开发者_如何学JAVAo play

plz help


According to this reference, just padding the link as an NSURL to [[UIApplication sharedApplication] openURL:myLink] should open the youtube app to the specified video.

Note that the simulator doesn't have the iPhone app, so this might only work on the device.


I found that it is very convenient to just load an HTML string. Basically like this (replace the variables according to your needs, and fill in the url of the video that youtube loads when displaying video as .mp4):

      NSString *htmlString = [NSString stringWithFormat:
    @"<body style=\"margin:0;background-color:#222222;\">
    <div style=\"padding:%i;width:%i;height:%i;background-color:#ffffff;\">
        <video width=\"%i\" height=\"%i\" controls=\"\" autoplay=\"\" tabindex=\"0\">
    <source src=\"%@\"></source>
        </video>
    </div>
    </body>",
     (int)margin, 
(int)(self.frame.size.width), 
(int)(self.frame.size.height), 
(int)videoWidth, 
(int)videoHeight, 
aURL];
        [objwebView loadHTMLString:htmlString baseURL:nil];

You can find the URL using Charles Debugging Proxy or something similar.


hey use iframe and it will play video in simulator and device both..implement it like this:

NSString *newHTML = @"<html>\
<style>body{padding:0;margin:0;}</style>\
<iframe width=\"640\" height=\"390\" src=\"http://www.youtube.com/embed/zL0CCsdS1cE\" frameborder=\"0\" allowfullscreen></iframe>\
</html>";
[mywebView loadHTMLString:newHTML baseURL:nil];  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜