Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-19-2011, 06:15 PM   #1 (permalink)
Registered Member
 
Twimfy's Avatar
 
Join Date: Mar 2009
Location: UK
Posts: 36
Twimfy is on a distinguished road
Send a message via MSN to Twimfy Send a message via Skype™ to Twimfy
Default [Tutorial] Embedding YouTube vids

YOU NEED A DEVICE TO RUN THIS CODE, IT DOES NOT WORK IN SIMULATOR!

THIS CODE HAS BEEN TESTED ON iOS 4.2 AND WORKS PERFECTLY.

Well I've seen a few people ask about embedding youtube videos on this forum and the links people have provided have either led to out of date code or tutorials on using locally stored videos.

This brief tutorial will provide you with the code to embed YouTube videos on multiple pages using the Tab Bar controller and show you how to modify the background image beyond the default white/transparent one that comes with the UIWebView.

Ultimately it will look something like this:



First thing you need to do is start a new project and pick window based application (although it won't really matter as we'll be building new classes from scratch). Name it whatever you want.

Keep the two appdelegate classes and delete the two view controllers.

Ensure that the UIToolkit is in frameworks, if not right click and choose 'add existing frameworks' and add it to the project.

Ok, click on your appdelegate.h and erase everything in it and replace it with:

Code:
#import <UIKit/UIKit.h>

@interface YourProjectAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;

@end
Make sure you change YourProjectAppDelegate to match whatever yours is called.

Next open appdelegate.m and delete everything, replace with the following:

Code:
#import "YourProjectAppDelegate.h"

@implementation YourProjectAppDelegate

@synthesize window;
@synthesize rootController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
	[window addSubview:rootController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
	[rootController release];
    [window release];
    [super dealloc];
}


@end
These two bits of code set up a tab bar controller as your root view controller. Simple stuff, they're provided in as a template but I prefer to do them from scratch just to be sure.

Now that's done. In the side view, find MainWindow.xib and double click it (you can delete the other .xib) close the iPhone window that appears and open the library. In objects select Tab Bar Controller and drag it to the MainWindow.xib box along with all of the other icons.

An iPhone window with tabs will appear. If you click the menu bar of the window the connections inspector will show you the list of pages (there are two by default) you can click the + and - to add or remove pages, just stick with two for now for simplicity.

EDIT: In order to change the background of your UIWebView, in the Media library drag a view into the window (not an image view - which will break the code) you can then add labels and texts to this view and change the background color to suit.

Ok back in xcode create two new classes. Rename them to PlayYouTubeInWebViewController.m PlayYouTubeInWebViewController.h and
PlayYoutTubeInWebViewController2.m PlayYouTubeInWebViewController2.h respectively.

In the two .h files delete all of the code and replace with this:

Code:
#import <UIKit/UIKit.h>


@interface PlayYouTubeInWebViewController : UIViewController {

}

@end
Remembering to add a '2' to PlayYouTubeInWebViewController when adding this code to the second .h file.

Then in the .m files replace all of the code with this:

Code:
#import "PlayYouTubeInWebViewController.h"


@interface PlayYouTubeInWebViewController (Private)
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;
@end


@implementation PlayYouTubeInWebViewController




- (void)viewDidLoad {
    [super viewDidLoad];

	

	
	[self embedYouTube:@"http://www.youtube.com/watch?feature=player_detailpage&v=7GoTB9MnFh8" frame:CGRectMake(20, 20, 70, 70)];
	[self embedYouTube:@"http://www.youtube.com/watch?v=nTasT5h0LEg&feature=player_detailpage" frame:CGRectMake(20, 120, 70, 70)];
}


- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
	NSString *embedHTML = @"\
    <html><head>\
	<style type=\"text/css\">\
	body {\
	background-color: transparent;\
	color: transparent;\
	}\
	</style>\
	</head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
	width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
	NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
	UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
	[videoView loadHTMLString:html baseURL:nil];
	[self.view addSubview:videoView];
	[videoView release];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
Again remembering to add a '2' to all instances of PlayYouTubeInWebViewController in the second .m file.

Okay lets take a look at this code. You can clearly see where the youtube links are placed. You can replace these with whatever youtube page you like. The CGRect co-ordinates determine the position of the youtube thumbnail and the location on the page. When we're finished you can play with these as much as you want and you will see what happens.

The CSS information in red simply deals with the style and appearance of the WebView and the controller for loading the video window. The default will load the video in fullscreen and when the video is finished or closed the window will release and the user will find themselves back on the thumbnail page...pretty neat huh?

If you want to add more videos simply repeat the line:

Code:
[self embedYouTube:@"http://www.youtube.com/watch?feature=player_detailpage&v=7GoTB9MnFh8" frame:CGRectMake(20, 20, 70, 70)];
Just replace the URL and adjust the co-ordinates or it will overlap a thumbnail in the existing position.

Ok, now to get the app working.

Open up MainWindow.xib to get back into the interface builder. Your Tab view should load by default. Select the first tab (again making sure the button is completely highlighted and not just the text) and open the inspector. Select the last tab in the inspector. There's a box which let's you choose a class from the drop down box. Select PlayYouTubeInWebViewController then highlight the second tab in the iPhone window and select PlayYouTubeInWebViewController2 in the same place in the inspector.

This will tell the app to load the WebView with the thumbnails when the tab button is pressed if you look in the CSS code for the YouTube code you will see the color and background color is set to transparent which allows the background imageview you made earlier to still be seen when the thumbnails load. I recommended a weird color just so you can be 100% sure you're seeing the imageview in the background and not a default color defined by the WebView class.

You might wonder why you can't change the transparent to another color and thus alter the background...well I'm not 100% sure why but it doesn't work like that. It took me a bit of time to realise I could just use a view as a background.

Now all you need to do is compile and run this on your device.

Hope this all makes sense. Have fun.

Last edited by Twimfy; 01-23-2011 at 11:56 AM. Reason: Improvements.
Twimfy is offline   Reply With Quote
Old 01-19-2011, 06:22 PM   #2 (permalink)
Registered Member
 
Twimfy's Avatar
 
Join Date: Mar 2009
Location: UK
Posts: 36
Twimfy is on a distinguished road
Send a message via MSN to Twimfy Send a message via Skype™ to Twimfy
Default

If you find any of this useful please let me know. Also let me know if there are any errors or bits which are hard to understand.

I will soon be posting more tutorials along with some video tutorials at http://twimfy.tumblr.com so if you're interested, come take a look.

- Thanks.

Whoops didn't realise this was a sub forum, if a mod could move it I'd appreciate it, thanks.

Last edited by Twimfy; 01-19-2011 at 06:43 PM.
Twimfy is offline   Reply With Quote
Old 01-19-2011, 08:23 PM   #3 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

I'm looking for more of making MPMoviePlayerController play a youtube video
__________________
iisword is offline   Reply With Quote
Old 01-19-2011, 08:25 PM   #4 (permalink)
Registered Member
 
Twimfy's Avatar
 
Join Date: Mar 2009
Location: UK
Posts: 36
Twimfy is on a distinguished road
Send a message via MSN to Twimfy Send a message via Skype™ to Twimfy
Default

Quote:
Originally Posted by iisword View Post
I'm looking for more of making MPMoviePlayerController play a youtube video
What would be the difference?
Twimfy is offline   Reply With Quote
Old 01-19-2011, 10:00 PM   #5 (permalink)
DSA
Registered Member
 
Join Date: Aug 2009
Posts: 26
DSA is on a distinguished road
Thumbs up

Nice I cant wait to give it a shot I'll PM with my results
DSA is offline   Reply With Quote
Old 01-19-2011, 10:14 PM   #6 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

I would rather call the video directly than with a webview. Anyway I found out what's going on, and I did make something that does work on MPMoviePlayerController, but the problem comes to find other videos.
__________________
iisword is offline   Reply With Quote
Old 01-20-2011, 08:03 AM   #7 (permalink)
Registered Member
 
Twimfy's Avatar
 
Join Date: Mar 2009
Location: UK
Posts: 36
Twimfy is on a distinguished road
Send a message via MSN to Twimfy Send a message via Skype™ to Twimfy
Default

Quote:
Originally Posted by iisword View Post
I would rather call the video directly than with a webview. Anyway I found out what's going on, and I did make something that does work on MPMoviePlayerController, but the problem comes to find other videos.
Ah ok I see. Yeah webviews are a bit of a pain, nevertheless I thought this was a reasonably tidy way of doing it.

The fact that it plays the video and then returns to the app in the same window is the main priority of this code. So many alternatives either open the YouTube app or a safari window and kill the app itself.
Twimfy is offline   Reply With Quote
Old 04-25-2012, 11:33 AM   #8 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 5
LorenzoP is on a distinguished road
Default

Why I can play video like this 1D Mania in the USA - YouTube and not like this One Direction Mania - YouTube ?

Where is the problem?
Thanks
LorenzoP is offline   Reply With Quote
Old 04-25-2012, 05:28 PM   #9 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Do you really need to post the same question to two different threads?

When publishing a YouTube video, the publisher can decide if it's allowed to be embedded or not. Presumably the second video is set to not allow embedding.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 04-26-2012, 05:27 AM   #10 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 5
LorenzoP is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
Do you really need to post the same question to two different threads?

When publishing a YouTube video, the publisher can decide if it's allowed to be embedded or not. Presumably the second video is set to not allow embedding.
Sorry for posting in two, I didn't now wich post was more active
you can remove the other.

Thanks for the answer, I thought there was a problem with the embedding code.
LorenzoP is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0