4

I want to play a youTube video using youtube video url.

I succeed in playing a video in iOS version 5.1.1 but the video does not played in iOS version 5.0.1 and 4.0 or below. I am using webview to play video.

Shawn Chin
  • 74,316
  • 17
  • 152
  • 184
Birju
  • 1,112
  • 11
  • 32
  • I am using shokwave-flash for playing youtube video.. – Birju Oct 03 '12 at 05:29
  • Flash doesn't work on iOS, you know that. – Jessedc Oct 03 '12 at 05:35
  • but it's work in ios 5.1.1.Any general framework is available for working in all version? – Birju Oct 03 '12 at 05:37
  • Have you seen other threads ? http://stackoverflow.com/questions/1779511/play-youtube-videos-with-mpmovieplayercontroller-instead-of-uiwebview – Jessedc Oct 03 '12 at 05:39
  • My requirement is playing video in UIWebView – Birju Oct 03 '12 at 05:41
  • Can you post some sample code for the shockwave code your using? – brynbodayle Oct 03 '12 at 12:21
  • My embed code is NSString *embedHTML = @"\ \ \ \ \ "; NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height]; – Birju Oct 04 '12 at 05:05

4 Answers4

2

for IOS below 5 you need to use iframe :

NSString *youTubeVideoHTML = @"<iframe id=\"yt\"    class=\"youtube-player\" type=\"text/html\" width=\"280\" height=\"186\" src=\"http://www.youtube.com/embed/xxxxxx\" frameborder=\"0\">";
and for ios 5 above we can use embed :  
   NSString *youTubeVideoHTML = @"<html><head>\
    <body style=\"margin:0\">\
    <embed id=\"yt\" src=\"http://www.youtube.com/watch?v=xxxxxx?autoplay=1\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

Note: In IOS 6 i found out that url works only if we pass youtube url as : http://www.youtube.com/v/xxxxxx?autoplay=1

Hope this works out.

Suhaiyl
  • 1,052
  • 9
  • 17
1

From your comment it looks like you are using the wrong embed code, youtube embeds now look like

<iframe width="560" height="315" src="http://www.youtube.com/embed/Cw2RzvK13F4" frameborder="0" allowfullscreen></iframe>

As mentioned before flash won't play on the iPhone, so it needs to use the HTML5 version, unfortunately I don't think 100% of youtube videos have been converted, but most should work.

Nick
  • 1,234
  • 8
  • 15
0

You need to make sure your using the proper URL format for embedded videos. They changed it somewhat recently.

Youtube Embedded Players and Player Parameters - YouTube - Google Developers

It looks like this:

http://www.youtube.com/embed/VIDEO_ID
brynbodayle
  • 6,376
  • 1
  • 31
  • 48
0

Here is my code which works well in all previous version of iOS 5.Just give a try with this code. I am sure this is going to fix your problem.

// Over here Just replace this url with your one's.
NSString *pdfString = @"http://www.youtube.com/watch?v=qe39vPFabuA";

NSString *htmlString = @"<html><head>\n"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 320\"/></head>\n"
"<body style=\"background:FFF;margin-top:0px;margin-left:0px\">\n"
"<div><object width=\"320\" height=\"416\">\n"
"<param name=\"movie\" value=\"%@\"></param>\n"
"<param name=\"wmode\" value=\"transparent\"></param>\n"
"<embed src=\"%@\"\n"
"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"416\"></embed>\n"
"</object></div></body></html>\n";

[webView loadHTMLString:[NSString stringWithFormat:htmlString,[pdfString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]],[pdfString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]] baseURL:nil];

Also, make sure to test the code in Device only. Testing on Simulator won't play the Video.

Ajay Sharma
  • 4,509
  • 3
  • 29
  • 59