Skip to content
Newer
Older
100644 54 lines (31 sloc) 3.32 KB
6128557 @steipete initial commit
authored
1 ## PSYouTubeExtractor ("BetterYouTube")
2
71ce38f @steipete Update README.md
authored
3 *Edit: This was a nice experiment, but there's now a better solution that doesn't involve UIWebViews: https://github.com/larcus94/LBYouTubeView*
4
5
6128557 @steipete initial commit
authored
6 Displaying YouTube is a pain in the ass. This class makes it a lot more bearable by trying to extract the native mp4 when available. If that doesn't work, we fall back to the UIWebView YouTube plugin.
7
8 There are two classes available:
9
10 ### PSYouTubeExtractor
11
f33ef26 @steipete more readme goodness
authored
12 Does some crazy things behind the scenes and extracts the mp4 of a YouTube video. I use an UIWebView to get the data, as Google does a pretty good job of obfuscating their html content.
6128557 @steipete initial commit
authored
13
14 [PSYouTubeExtractor extractorForYouTubeURL:self.youTubeURL success:^(NSURL *URL) {
15 NSLog(@"Finished extracting: %@", URL);
16 // show the movie!
17 } failure:^(NSError *error) {
18 NSLog(@"Failed to query mp4: %@", error);
19 }];
20
a489ed4 @steipete readme stuff
authored
21 Note that PSYouTubeExtractor is *not* a NSOperation, as there's some craziness behind the scenes that needs a RunLoop (and I don't wanna mess around with runloops in NSOperation). The class retains itself until either success or failure is called, or until you send cancel to it. The blocks are nullified afterwards, so don't worry about retain cycles. (You still have to worry about Xcode bit chin' about it.)
6128557 @steipete initial commit
authored
22
a489ed4 @steipete readme stuff
authored
23 ### PSYouTubeView
6128557 @steipete initial commit
authored
24
25 Woohoo! That's where the awesomeness is. Just use this instead of your UIWebView and you're good.
26
a489ed4 @steipete readme stuff
authored
27 NSURL *youTubeURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=Vo0Cazxj_yc"];
28
29 PSYouTubeView *youTubeView = [[PSYouTubeView alloc] initWithYouTubeURL:youTubeURL
30 frame:CGRectMake(0,0,200,200)
31 showNativeFirst:YES];
6128557 @steipete initial commit
authored
32 [self.view addSubview:youTubeView];
33
34 Note that you should set the correct frame right away. If we need to fallback to UIWebView, the YouTube plugin can't resize. (You can recreate it, but that would kill a running video). However, in most cases it should extract the mp4 successfully and you don't need to worry about that crap.
35
36 The setting 'showNativeFirst' decides if you want to start with a MPMoviePlayerController or a UIWebView. As we are optimistic, I suggest you set this to YES per default.
37
38 I am using this class for [PSPDFKit](http://pspdfkit.com), my pdf framework where you can add interactive elements, and YouTube just sucked too much, so I wrote this helper. That's also why there is a block for "setupNativeView" and "setupWebView", you can override those and do your own custom stuff with it.
39
a489ed4 @steipete readme stuff
authored
40 ### ARC
c624a29 @steipete add note about iOS versions
authored
41 … what else? Also should work with iOS 4.0 upwards. Tested with iOS 4.3 and 5.0.
a489ed4 @steipete readme stuff
authored
42
6128557 @steipete initial commit
authored
43 ### Help wanted!
44
45 If anyone has a better way of extracting the final YouTube mp4 url (maybe some crazy regex magic), it would make the class a lot faster (we could get rid of the UIWebView). I am kinda ok with the UIWebView solution though, as this one will be pretty robust. I look for a <video> tag, and as long as Google shows a video on a YouTube page, we find the source.
46
47 Also, we could add support for Reachability to re-try the extracting in case we didn't had network when the view was created. Feel free to send a pull request!
48
f33ef26 @steipete more readme goodness
authored
49 PS: If you like this stuff, [you should follow me on Twitter!](http://twitter.com/steipete)
50
6128557 @steipete initial commit
authored
51
52 ### License
53
54 MIT! See LICENSE file for the legal stuff.
Something went wrong with that request. Please try again.