How do you play YouTube videos on iPhone, post July 2010?
There was an official way to do this, from the YouTube folks.
(link to YT official way - NB: now broken, but YT hasn't (bothered to) correct this page: http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html)
In July 2010, they deliberately removed that - it no longer works (I've got an app that worked fine prior to the change, and now doesn't, using YT's own code).
There's a "new" way that the YouTube folks described as "experimental" - but it's only开发者_Python百科 partially working. Where you could previously put YT videos into your iPhone GUI, now you cannot - they are "required" to be given an entire screen to themselves.
(link to YT official "new" way: http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html)
(link to YT official thread, that ends with "it might work... eventually": http://code.google.com/apis/youtube/forum/discussion.html?place=topic%2Fyoutube-api-gdata%2FtMYvulpGUow%2Fdiscussion)
Worse, YouTube engineers didn't provide any user feedback; when the user clicks on a button, you have to wait for the YT servers to process a couple of web lookups (you can see this if you look at your console log files).
All the user sees is: "this app appears to have crashed".
Argh! Is there any other way? What are other people using here that's working still - on OS 3.x + 4.x - since the recent YT changes?
I currently use code similar to this for embedding youtube videos into apps. However, when a user taps on the video (generates a thumbnail the user can tap on in the webview) it goes full screen, so may not be what you're looking for.
NSMutableString* urlString = [NSMutableString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos/%@?v=2&alt=jsonc", youtubeId];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// Extract JSON from data into an NSDictionary
NSDictionary* jsonDict = /* however you want to do it */
// Make sure that the video hasn't been flagged as restricted, otherwise the user won't know what's going on. Later check restricted and present an error as appropriate.
BOOL restricted = youtubeVideoRestricted(jsonDict);
If restricted
is NO
, then I feed the web view some html like this:
embedSrc = @"<object width=\"640\" height=\"385\">"
"<param name=\"movie\" value=\"http://www.youtube.com/v/%@\"></param>"
"<embed src=\"http://www.youtube.com/v/%@\" type=\"application/x-shockwave-flash\" width=\"640\" height=\"385\"></embed>"
"</object>";
embedSrc = [NSString stringWithFormat:embedSrc, youtubeId, youtubeId];
I have this in an app on the store right now, works fine. That said, I've learned to keep the video up. So in case of problems with the youtube feed, it falls back to the blip.tv service where the video lives as well. User doesn't notice something failed on youtube. But that's beyond the scope of your question.
精彩评论