<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[iPhoneiOS dev:tips]; &#187; Video</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/video/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Play Movies from the Application Bundle or from a Remote Server (URL)</title>
		<link>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html#comments</comments>
		<pubDate>Wed, 21 Jul 2010 00:51:45 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6691</guid>
		<description><![CDATA[In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : Display iPhone Movies in Portrait Mode and Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : <a href="http://iphonedevelopertips.com/video/display-iphone-movies-in-portrait-mode-updated.html">Display iPhone Movies in Portrait Mode</a> and <a  target="_blank"  href="http://iphonedevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html">Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK</a> </p>
<p>For both posts, in order to create a working example that one could download and run as is, the code examples referenced a movie that I included in the application bundle. Although handy, this prompted a number of questions about how to play a movie from a remote resource.</p>
<p>I&#8217;ve updated the original examples for both posts, with an option to play movies from the application bundle or from a remote server.<br />
<span id="more-6691"></span></p>
<h5>Play Movie from Server</h5>
<p>The first change I made was in <strong>CustomMoviePlayerViewController.m</strong>, where I added a method to initialize the movie player with a NSURL object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>URL
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> URL;
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>moviePath
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>moviePath<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For both methods, the end result is an initialized NSURL object.</p>
<p>The other code change is in <strong>TestViewController.m</strong> where I updated the <strong>loadMoviePlayer</strong> method to support calling either of the above methods:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadMoviePlayer
<span style="color: #002200;">&#123;</span>  
<span style="color: #11740a; font-style: italic;">/*
  // Play movie from app bundle
  NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;Movie-1&quot; ofType:@&quot;mp4&quot; inDirectory:nil];
  moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:path] autorelease];
*/</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Play movie from URL</span>
  <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://someurlsomewhere.com/movie.mp4&quot;</span><span style="color: #002200;">&#93;</span>;
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomMoviePlayerViewController alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show the movie player as modal</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>moviePlayer animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Prep and play the movie</span>
  <span style="color: #002200;">&#91;</span>moviePlayer readyPlayer<span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Comment in/out the relevant code in lines 4 &#8211; 11 to swap between playing from a file in the bundle and a movie accessible on a remote server.</p>
<h5>Download Movie Player Source Code</h5>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4-Landscape.zip">MoviePlayer in Landscape Mode</a></p>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4-Portrait.zip">MoviePlayer in Portrait Mode (requires 3.2 OS and greater)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Display iPhone Movies in Portrait Mode (Updated)</title>
		<link>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html#comments</comments>
		<pubDate>Wed, 14 Jul 2010 00:36:36 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6652</guid>
		<description><![CDATA[In the previous post, Getting MPMoviePlayerController to Cooperate with iOS4 and Earlier Versions of iPhone SDK, I demonstrated how to work with the most recent changes in iPhone OS 3.2 and 4.0 MPMoviePlayerController to enable one application to play movies regardless of the OS version on the device. In this post I want to show [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post, <a  target="_blank"  href="http://iphonedevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html">Getting MPMoviePlayerController to Cooperate with iOS4 and Earlier Versions of iPhone SDK</a>, I demonstrated how to work with the most recent changes in iPhone OS 3.2 and 4.0 <strong>MPMoviePlayerController</strong> to enable one application to play movies regardless of the OS version on the device.</p>
<p>In this post I want to show you a few minor changes you can make to the previous app to display a movie in portrait mode, something that was not possible in earlier (pre 3.2) OS versions. With that said, back in January of 2010 I did cover a means to create the illusion that a movie was in portrait mode, if this piques your interest, have a look here: <a  target="_blank"  href="http://iphonedevelopertips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html">Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API’s</a><br />
<span id="more-6652"></span></p>
<p>The figure below shows the same movie as played back in landscape and portrait:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/portraitMode.png" /></p>
<h5>Play Movies in Portrait Mode</h5>
<p>The code below is from the previous post and shows how to configure the movie player view to display in landscape:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">MPMoviePlayerController <span style="color: #002200;">*</span>mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span>
     initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
...
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  ...
  <span style="color: #11740a; font-style: italic;">// Rotate the view for landscape playback</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBounds<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setTransform<span style="color: #002200;">:</span>CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span>M_PI <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set frame of movieplayer</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>To playback a movie in portrait mode is as easy as removing the code for the rotation and changing the frame of the movieplayer. I&#8217;ve also added a call to scale the movie so it fills the screen, this will maintain the aspect ratio.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  ...
  <span style="color: #11740a; font-style: italic;">// Rotate the view for landscape playback</span>
  <span style="color: #11740a; font-style: italic;">//	  [[self view] setBounds:CGRectMake(0, 0, 480, 320)];</span>
  <span style="color: #11740a; font-style: italic;">//		[[self view] setCenter:CGPointMake(160, 240)];</span>
  <span style="color: #11740a; font-style: italic;">//		[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; </span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set frame of movieplayer</span>
  <span style="color: #11740a; font-style: italic;">// [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">160</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Scale uniformly, maintaining aspect ratio</span>
  <span style="color: #002200;">&#91;</span>mp setScalingMode<span style="color: #002200;">:</span>MPMovieScalingModeAspectFill<span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This approach works on 3.2 (iPad) as well as 4.0 and greater. Needless to say, given the large display on the iPad this is a nice alternative as the user can watch videos without rotating the device.</p>
<h5>Source Code</h5>
<p><a href="http://iphonedevelopertips.com/wp-content/uploads/2010/07/MoviePlayer-Portrait-Mode-ios4.zip">Download the source code</a> to build an iPhone app that displays movies in portrait mode.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting MPMoviePlayerController to Work with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK</title>
		<link>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html#comments</comments>
		<pubDate>Fri, 09 Jul 2010 02:52:29 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6579</guid>
		<description><![CDATA[The API and overall approach for working with MPMoviePlayerController has changed just enough in the 3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I&#8217;ll walk through a short example of a movie player application that will work with 3.x, [...]]]></description>
			<content:encoded><![CDATA[<p>The API and overall approach for working with <strong>MPMoviePlayerController</strong> has changed just enough in the  3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I&#8217;ll walk through a short example of a movie player application that will work with 3.x, 3.2 and 4.0 SDKs. </p>
<p>In the next post, <a href="http://iphonedevelopertips.com/video/display-iphone-movies-in-portrait-mode-updated.html">Display iPhone Movies in Portrait Mode</a>, I&#8217;ll show how to create a movie player that runs in a view that is <em>not</em> fullscreen as well as how to show a few lines of code to display a movie in portrait mode &#8211; the one caveat here is that both of the tips in the second post will apply only to OS versions 3.2 and up.<br />
<span id="more-6579"></span></p>
<h5>What&#8217;s Changed with MPMoviePlayerController in 3.2 and 4.0?</h5>
<p>There are any number of changes in the <strong>MPMoviePlayerController</strong>, however, a few standout as potential roadblocks to getting a movie to display:</p>
<p>- In 3.1 and earlier versions, <strong>MPMoviePlayerController</strong> was full-screen only. Playing a movie was straight-forward, create a player, initialize with a file (path or URL) and call a method to start playback &#8211; the rest was taken care of for you.</p>
<p>- With 3.2 and later, movies can playback in fullscreen or a custom view, as well as portrait or landscape.</p>
<p>- The notification <strong>MPMoviePlayerContentPreloadDidFinishNotification</strong>  has been deprecated. This notification was used in earlier versions to notify observers that a movie was loaded and ready to play.</p>
<h5>The Movie Application</h5>
<p>The application in this post is quite simple, it consists of a view controller with nothing more than a button to start playback and a second view controller to manage a <strong>MPMoviePlayerController</strong> and the <strong>NSURL</strong> of the movie. The two views are shown in the figures below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/movie1.png" /></p>
<h5>Primary View Controller</h5>
<p>The interface definition for the primary view is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@class</span> CustomMoviePlayerViewController;
&nbsp;
<span style="color: #a61390;">@interface</span> TestViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  CustomMoviePlayerViewController  <span style="color: #002200;">*</span>moviePlayer;
  UIButton  <span style="color: #002200;">*</span>playButton;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>CustomMoviePlayerViewController</strong> is the controller for managing the movie, we&#8217;ll look at that code in a moment. </p>
<p>In the code below we create the view, add a play button, create a method for processing a button press event and within <strong>loadMoviePlayer</strong>, we get a reference to the movie file and create an instance of the <strong>CustomMoviePlayerViewController</strong>, which will load and play the movie.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadView
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Setup the view</span>
  <span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor grayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setUserInteractionEnabled<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add play button </span>
  playButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">53</span>, <span style="color: #2400d9;">212</span>, <span style="color: #2400d9;">214</span>, <span style="color: #2400d9;">36</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
  <span style="color: #002200;">&#91;</span>playButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;playButton.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>playButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;    
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>playButton<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// If pressed, play movie</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> playButton<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>self loadMoviePlayer<span style="color: #002200;">&#93;</span>;	
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadMoviePlayer
<span style="color: #002200;">&#123;</span>  
  <span style="color: #11740a; font-style: italic;">// Play movie from the bundle</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Movie-1&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mp4&quot;</span> inDirectory<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create custom movie player   </span>
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomMoviePlayerViewController alloc<span style="color: #002200;">&#93;</span> initWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show the movie player as modal</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>moviePlayer animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Prep and play the movie</span>
  <span style="color: #002200;">&#91;</span>moviePlayer readyPlayer<span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Notice that the movie for this example is loaded from the application bundle. Also, once the custom movie player is created, the view is shown as a modal view.</p>
<h5>Custom Movie Player View Controller</h5>
<p>The interface definition for the movie view controller is below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> CustomMoviePlayerViewController <span style="color: #002200;">:</span> UIViewController 
<span style="color: #002200;">&#123;</span>
  MPMoviePlayerController <span style="color: #002200;">*</span>mp;
  <span style="color: #400080;">NSURL</span>  <span style="color: #002200;">*</span>movieURL;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>moviePath;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>readyPlayer;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The initialization code is where the movie player and associated URL are created. The primary goal of the initialization is to create the <strong>NSURL</strong> needed by the movie player.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>moviePath
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Initialize and create movie URL</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>moviePath<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code to create the player and setup the notifications is where we start to deal with the differences in how the movie player works on various OS versions. Notice below the call to <strong>respondsToSelector</strong>, this is the Apple recommended way to check for feature availability, versus looking for a specific OS version. </p>
<p>For devices running 3.2 and above, the movie player controller has a method named <strong>loadstate</strong>, if this exists we can access a few additional methods to set the player to fullscreen as well as request the movie to begin preloading.  </p>
<p>Equally important is the distinction of which notification to setup &#8211; see lines 16 and 24.</p>
<p>You can read more about managing different OS version in this post <a  target="_blank"  href="http://iphonedevelopertips.com/xcode/base-sdk-and-iphone-os-deployment-target-developing-apps-with-the-4-x-sdk-deploying-to-3-x-devices.html">Developing iPhone Apps with iOS4 SDK, Deploying to 3.x Devices</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> readyPlayer
<span style="color: #002200;">&#123;</span>
   mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For 3.2 devices and above</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mp respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadState<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Set movie player layout</span>
    <span style="color: #002200;">&#91;</span>mp setControlStyle<span style="color: #002200;">:</span>MPMovieControlStyleFullscreen<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mp setFullscreen<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// May help to reduce latency</span>
    <span style="color: #002200;">&#91;</span>mp prepareToPlay<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Register that the load state changed (movie is ready)</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerLoadStateDidChangeNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>  
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie is in memory and ready to play.</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie has finished playing. </span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerPlaybackDidFinishNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next two chunks of code are for each of the selectors, one for each notification. The code below is for earlier OS versions &#8211; nothing more than removing the notification and asking the movie to play.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged: 
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Remove observer</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> 	defaultCenter<span style="color: #002200;">&#93;</span> 
    removeObserver<span style="color: #002200;">:</span>self
    name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification
    object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Play the movie</span>
   <span style="color: #002200;">&#91;</span>mp play<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For the notification generated in 3.2 and above, there are a few more details to tend to:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
* For 3.1.x devices see moviePreloadDidFinish:
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Unless state is unknown, start playback</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mp loadState<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> MPMovieLoadStateUnknown<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Remove observer</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
      removeObserver<span style="color: #002200;">:</span>self
      name<span style="color: #002200;">:</span>MPMoviePlayerLoadStateDidChangeNotification 
      object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// When tapping movie, status bar will appear, it shows up</span>
    <span style="color: #11740a; font-style: italic;">// in portrait mode by default. Set orientation to landscape</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> setStatusBarOrientation<span style="color: #002200;">:</span>UIInterfaceOrientationLandscapeRight animated<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Rotate the view for landscape playback</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBounds<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setTransform<span style="color: #002200;">:</span>CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span>M_PI <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Set frame of movie player</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Add movie player as subview</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;   
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Play the movie</span>
    <span style="color: #002200;">&#91;</span>mp play<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Beyond removing the notification, we also adjust the status bar, rotate the view, set the frame of the movie player, add the movie player as a subview in the view controller and wrap it all up by asking the movie to play.</p>
<p><strong>Note:</strong> MPMoviePlayerViewController is also an option over creating your own view controller as I&#8217;ve done here.</p>
<h5>Source Code</h5>
<p>The easiest way to see all this working is to <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4.zip">download the source code</a> and step through the code in the debugger.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Display YouTube Videos Without Exiting Your Application</title>
		<link>http://iPhoneDeveloperTips.com/video/display-youtube-videos-without-exiting-your-application.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/display-youtube-videos-without-exiting-your-application.html#comments</comments>
		<pubDate>Wed, 19 May 2010 11:43:23 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6274</guid>
		<description><![CDATA[If you&#8217;d like to play a YouTube video inside your application there are two common ways to do this, by launching the YouTube player and by using a UIWebview. Launch Native YouTube Application This approach will exit your application and begin the YouTube player on the iPhone: &#91;&#91;UIApplication sharedApplication&#93; openURL:&#91;NSURL URLWithString:@&#34;http://www.youtube.com/watch?v=gczw0WRmHQU&#34;&#93;&#93;; Clickable Thumbnail and UIWebview [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;d like to play a YouTube video inside your application there are two common ways to do this, by launching the YouTube player and by using a UIWebview.</p>
<h5>Launch Native YouTube Application</h5>
<p>This approach will exit your application and begin the YouTube player on the iPhone:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> 
   openURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/watch?v=gczw0WRmHQU&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><span id="more-6274"></span></p>
<h5>Clickable Thumbnail and UIWebview</h5>
<p>If you&#8217;ve noticed in Safari on the iPhone, when the browser finds a YouTube video reference, a clickable link is created, which will start the movie when tapped without leaving Safari.</p>
<p>We can use the same approach within an iPhone application using a UIWebView, including a clickable link with a poster frame from the movie.</p>
<p>For this example I have created a separate class that subclasses UIWebView:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> YouTubeView <span style="color: #002200;">:</span> UIWebView 
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>YouTubeView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithStringAsURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>urlString frame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Here&#8217;s the implementation of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;YouTubeView.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> YouTubeView
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialization</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>YouTubeView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithStringAsURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>urlString frame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Create webview with requested frame size</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWebView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// HTML to embed YouTube video</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>youTubeVideoHTML <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&lt;html&gt;&lt;head&gt;<span style="color: #2400d9;">\</span>
                          &lt;body style=<span style="color: #2400d9;">\&quot;</span>margin:0<span style="color: #2400d9;">\&quot;</span>&gt;<span style="color: #2400d9;">\</span>
                          &lt;embed id=<span style="color: #2400d9;">\&quot;</span>yt<span style="color: #2400d9;">\&quot;</span> src=<span style="color: #2400d9;">\&quot;</span>%@<span style="color: #2400d9;">\&quot;</span> type=<span style="color: #2400d9;">\&quot;</span>application/x-shockwave-flash<span style="color: #2400d9;">\&quot;</span> <span style="color: #2400d9;">\</span>
                          width=<span style="color: #2400d9;">\&quot;</span>%0.0f<span style="color: #2400d9;">\&quot;</span> height=<span style="color: #2400d9;">\&quot;</span>%0.0f<span style="color: #2400d9;">\&quot;</span>&gt;&lt;/embed&gt;<span style="color: #2400d9;">\</span>
                          &lt;/body&gt;&lt;/html&gt;&quot;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Populate HTML with the URL and requested frame size</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>html <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span>youTubeVideoHTML, urlString, frame.size.width, frame.size.height<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Load the html into the webview</span>
    <span style="color: #002200;">&#91;</span>self loadHTMLString<span style="color: #002200;">:</span>html baseURL<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Cleanup</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The magic here is the HTML for embedding the video content, and notice how the HTML is populated with the video URL and the desired frame size.</p>
<p>We can now insert this view inside a ViewController class as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create view that will act as link to youtube video,</span>
  <span style="color: #11740a; font-style: italic;">// centering the view </span>
  YouTubeView <span style="color: #002200;">*</span>youTubeView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>YouTubeView alloc<span style="color: #002200;">&#93;</span> 
    initWithStringAsURL<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/watch?v=gczw0WRmHQU&quot;</span> 
    frame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">170</span>, <span style="color: #2400d9;">120</span>, <span style="color: #2400d9;">120</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>youTubeView<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here is how the clickable link looks, the image on the left is before the poster frame has downloaded, the right includes the poster frame:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/05/youtube.jpg" /></p>
<p>Tapping the link will start the movie in a webview.</p>
<h5>Project Source Code</h5>
<p>You can download the source code here: <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/05/YouTubeiPhoneApp.zip">Display YouTube Video Inside an iPhone Application</a>.</p>
<p><strong>Note:</strong> As Joao mentions in a comment below, this code will not run on the simulator.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/display-youtube-videos-without-exiting-your-application.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API&#8217;s</title>
		<link>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:26:13 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5020</guid>
		<description><![CDATA[It&#8217;s been covered by a number of websites and blogs on how to play movies in portrait mode using MPMoviePlayerController. Problem is, every solution that I&#8217;ve been able to find uses private API&#8217;s to tell the player to flip the direction of play. Other than the built-in movie player, another option is to use a [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been covered by a number of websites and blogs on how to play movies in portrait mode using <strong>MPMoviePlayerController</strong>. Problem is, every solution that I&#8217;ve been able to find uses private API&#8217;s to tell the player to flip the direction of play.</p>
<p>Other than the built-in movie player, another option is to use a <strong>UIWebview</strong>, however, there are a few drawbacks including no support for notifications on when a movie has been preloaded, which is handy for displaying a &#8220;please wait&#8221; loading message.</p>
<p>This post is introduces another approach to playing a movie in portrait mode without delving into private API&#8217;s, and I think you&#8217;ll agree, it&#8217;s quite clever.<br />
<span id="more-5020"></span></p>
<h5>Portrait Video Mode on the iPhone</h5>
<p>I spend a fair amount of time working with video related applications and content, so when I saw a portrait video playing seamlessly inside <a  target="_blank"  href="http://itunes.apple.com/us/app/dog-tricks-best-101-dog-tricks/id311117783?mt=8">Dog Tricks &#8211; Best of 101 Dog Tricks</a> I was very interested to figure out how to do the same.</p>
<p>I wrote Michael Schneider of <a  target="_blank"  href="http://www.hivebrainsoftware.com/">Hive Brain Software</a>, developer of the above app and asked if he would share his secret. Michael was kind enough to pass along some great notes explaining how we got this to work. This post walks through a complete working example I wrote based on the information Michael shared with me.</p>
<h5>The End Result</h5>
<p>Let&#8217;s start by looking at a short video of the application I&#8217;ll build in this post running in the simulator:</p>
<p><embed src="http://iphonedevelopertips.com/wp-content/uploads/2010/01/port.mov" width="320" height="430" autoplay="false"> </p>
<p>Notice how the video is shown within the application with the tabbar controls still visible on the screen.</p>
<h5>The 30,000 Foot View</h5>
<p>The basic idea to make this work is simple, rotate the orientation of the original video, turn off the movie player controls and fire up the movie player as normal.</p>
<p>What you end up with is the movie player running landscape mode with your video content viewable in portrait mode.  The reason for hiding the controls is that they would appear sideways. As an example, notice in the screenshot below that the video is viewable in portrait mode, however, the controls are still oriented for a landscape movie player:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp51-201x300.png" alt="" title="mp5" width="201" height="300" class="alignleft size-medium wp-image-5204" style="margin: 10px;"/></p>
<p>Although a simple trick, it&#8217;s the little things that will make this approach appear seamless within your application. As an example of a nice effect, I&#8217;ll show how to overlay an image on top of the movieplayer so it appears as though the movie is playing directly inside your app.</p>
<p>There are three steps to make this all work:</p>
<p>- Change the orientation of the movie<br />
- Turn off the movie player controls<br />
- Overlay an image of your application UI on top of the movie player</p>
<p>Let&#8217;s see how to pull all the pieces together&#8230;</p>
<h5>Video Orientation</h5>
<p>The first step is to use a capable video editing tool to rotate your video to portait mode. I used Quicktime Pro (on Snow Leopard), the steps that I used follow:</p>
<p>- From the Window menu choose Show Movie Properties<br />
- Click on the row for the video track<br />
- Click the Visual Settings tab<br />
- In the Flip/Rotate section, rotate the video counter-clockwise<br />
   (or clockwise depending on the orientation you set for your iPhone app).</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/mp2.png" alt="" title="mp2" width="300" height="228" class="alignnone size-full wp-image-5111" style="margin: 10px;"/></p>
<h5>Code for App Delegate</h5>
<p>The app delegate code for this code example is nothing more than a Window object and a Tabbar controller. The interface and implementation files are below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@class</span> TabbarController;
&nbsp;
<span style="color: #a61390;">@interface</span> Test_appAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UIWindow <span style="color: #002200;">*</span>window;
  TabbarController <span style="color: #002200;">*</span>tabbar;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> TabbarController <span style="color: #002200;">*</span>tabbar;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The implementation file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>   
  <span style="color: #11740a; font-style: italic;">// Create and initialize the window</span>
  window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create the tabbar</span>
  tabbar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TabbarController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>tabbar.view<span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
...</pre></div></div>

<h5>Code for Tabbar Controller</h5>
<p>The tabbar controller is code is nothing out of the ordinary, the interface file is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TabbarController <span style="color: #002200;">:</span> UITabBarController  &lt;UITabBarControllerDelegate&gt;
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>As you can see in the code below I&#8217;ve defined three tabs, each with a unique title:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> TabbarController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
  self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    self.delegate <span style="color: #002200;">=</span> self;
&nbsp;
    UIViewController <span style="color: #002200;">*</span>localViewController;   
&nbsp;
    <span style="color: #11740a; font-style: italic;">// New tabbar controller and array to contain the view controllers</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>localViewControllersArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 1</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar1ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 1&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 2</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar2ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 2&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 3</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar3ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 3&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// Retained by above array</span>
&nbsp;
    self.viewControllers <span style="color: #002200;">=</span> localViewControllersArray;
    <span style="color: #002200;">&#91;</span>localViewControllersArray release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Code for Tab 1</h5>
<p>The only tabbar that has any working code for this example is the left-most tab, which plays the movie in portrait mode. The interface definition for this tab is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;MediaPlayer/MediaPlayer.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Tabbar1ViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  MPMoviePlayerController  <span style="color: #002200;">*</span>moviePlayer;
  <span style="color: #400080;">NSURL</span>  <span style="color: #002200;">*</span>movieURL;
  MovieOverlayViewController <span style="color: #002200;">*</span>overlay;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Inside the implementation file, the first block of relevant code is the initialization, which sets up the view, the background color and the title for the tab:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theTitle 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;    
    self.view.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blackColor<span style="color: #002200;">&#93;</span>;
    self.title <span style="color: #002200;">=</span> theTitle;
  <span style="color: #002200;">&#125;</span>  
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The next section of code to write is when the tab is about to appear, here we create the path to the movie, setup the movie player, register to receive a notification when the movie has finished playing, start the movie, and finally, overlay an image on top of the player (more on that below):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Path to the movie</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mov&quot;</span><span style="color: #002200;">&#93;</span>;      
  movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup the player</span>
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Hide the controls</span>
  <span style="color: #002200;">&#91;</span>moviePlayer setMovieControlMode<span style="color: #002200;">:</span>MPMovieControlModeHidden<span style="color: #002200;">&#93;</span>;  
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie has finished playing</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                      selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                      name<span style="color: #002200;">:</span>MPMoviePlayerPlaybackDidFinishNotification 
                      object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start the movie  </span>
  <span style="color: #002200;">&#91;</span>moviePlayer play<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Overlay an image with center that is transparent for movie to show through  </span>
  overlay <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MovieOverlayViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
   <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>windows <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> windows<span style="color: #002200;">&#93;</span>;
  UIWindow <span style="color: #002200;">*</span>moviePlayerWindow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>windows objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>moviePlayerWindow addSubview<span style="color: #002200;">:</span>overlay.view<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The interesting code starts on line 21, this is where I define a new view controller, <strong>MovieOverlayViewController</strong>, which will overlay an image on top of the running movie player. This overlay will present the illusion that the movie is running directly inside the app.</p>
<h5>Movie Image Overlay</h5>
<p>To give the app the appearance of running natively in portrait mode, we&#8217;ll overlay an image that shows the tabbars along the bottom and a text banner across the top. I&#8217;ll also write code to detect when the user taps on the image and do the necessary checks to see if the user tapped in an area that would translate to one of the tabs, more on that momentarily.</p>
<p>The finished look on the device is below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp6-200x300.png" alt="" title="mp6" width="200" height="300" class="alignnone size-medium wp-image-5207" /></p>
<p>Notice in the figure below that actual image we will overlay is nothing more than a transparent image with some text across the top and a series of tabbar images across the bottom.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp7-200x300.png" alt="" title="mp7" width="200" height="300" class="alignnone size-medium wp-image-5208" /></p>
<p>The movie overlay view controller for this app is quite trivial, it contains nothing more than a <strong>UIImageView</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MovieOverlayViewController <span style="color: #002200;">:</span> UIViewController 
<span style="color: #002200;">&#123;</span>
  UIImageView <span style="color: #002200;">*</span>overlay;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Inside the implementation file, we begin with the initialization and code for creating the <strong>UIImageView</strong> that will be overlayed on the movie player:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;    
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
  overlay <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;overlay.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>overlay<span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The other relevant code for this view controller is managing touches on the image. To provide the most realistic user experience, once the image is overlayed and the movie is playing, you will need to detect touches on the image where the tabs live. The code below will determine if a touched point is within the rectangle of each of the tabs, printing a message to the console on which tab was touched.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event 
<span style="color: #002200;">&#123;</span>	  
  <span style="color: #11740a; font-style: italic;">// Detect touch anywhere</span>
  UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Where is the point touched</span>
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>; 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pointx: %f pointy:%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Was a tab touched, if so, which one...</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 1 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">107</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 2 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">214</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 3 touched&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>You could add code to the above example and when tab 2 or 3 is tapped, stop the movie, remove the overlay and switch to the view controller associated with the appropriate tab.</p>
<h5>Xcode Portrait Mode Video Source Code</h5>
<p>You can download the entire Xcode project here: <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/XcodePortraitVideo.zip">Xcode Portrait Video</a>.</p>
<h5>Credits</h5>
<p>Thanks again to <a  target="_blank"  href="http://www.hivebrainsoftware.com/">Michael Schneider of Hive Brain Software</a> for sharing his insight on this trick. You can check out more of Michael&#8217;s work on a series of self improvement applications at <a  target="_blank"  href="http://www.relaxingapps.com/">Relaxing Apps</a>.</p>
<p>And Michael would like to pass on many thanks Scott Michaels at Atimi in Vancouver BC for sharing information on this approach at the 360iDev conference in San Jose.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2010/01/port.mov" length="306215" type="video/quicktime" />
		</item>
	</channel>
</rss>
