Play youtube video in iphone app in ios version lower than 5.1.1?

I want to play youTube video using the YouTube URL.

I was able to play the video in iOS version 5.1.1, but the video does not play in iOS version 5.0.1 and 4.0 or lower. I use webview to play videos.

+4
source share
4 answers

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 the url only works if we pass the youtube URL as: http://www.youtube.com/v/xxxxxx?autoplay=1

Hope this works.

+2
source

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

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

As already mentioned, flash will not play on the iPhone, so it needs to use the HTML5 version, unfortunately, I donโ€™t think that 100% of YouTube videos were converted, but most of them should work.

+1
source

You need to make sure that you are using the correct URL format for embedded videos. They recently changed it.

Youtube and Embedded Player Options - YouTube - Google Developers

It looks like this:

 http://www.youtube.com/embed/VIDEO_ID 
0
source

Here is my code that works well in all previous versions of iOS 5. Just try this code. I am sure this will 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]; 

In addition, be sure to check the code only on the device . Testing on Simulator will not play the video.

0
source

Source: https://habr.com/ru/post/1434195/


All Articles