<?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>iPhone Development Blog &#187; Audio &amp; Video</title>
	<atom:link href="http://iPhoneIncubator.com/blog/category/audio-video/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneIncubator.com/blog</link>
	<description>Tips and Tricks for iPhone, iPod, iPad and iOS Developers</description>
	<lastBuildDate>Fri, 09 Jul 2010 16:10:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Play Video with MPMoviePlayerController in iOS 3.0 and 3.2/4.0</title>
		<link>http://iPhoneIncubator.com/blog/audio-video/play-video-with-mpmovieplayercontroller-in-ios-3-0-and-3-24-0</link>
		<comments>http://iPhoneIncubator.com/blog/audio-video/play-video-with-mpmovieplayercontroller-in-ios-3-0-and-3-24-0#comments</comments>
		<pubDate>Fri, 09 Jul 2010 16:07:24 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Audio & Video]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[MPMoviePlayerController]]></category>

		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=510</guid>
		<description><![CDATA[Apple made significant changes to the MPMoviePlayerController API:s in iOS 3.2. Before, all you had to do was to initialize the MPMoviePlayerController and call play. Now things are a bit more complicated because videos can be played on a portion of the screen (good for the iPad) and not just in full screen mode.
But if all you [...]<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/play-video-with-mpmovieplayercontroller-in-ios-3-0-and-3-24-0">Play Video with MPMoviePlayerController in iOS 3.0 and 3.2/4.0</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Apple made significant changes to the MPMoviePlayerController API:s in iOS 3.2. Before, all you had to do was to initialize the MPMoviePlayerController and call play. Now things are a bit more complicated because videos can be played on a portion of the screen (good for the iPad) and not just in full screen mode.</p>
<p>But if all you want is to play a video in full-screen mode on all versions of the OS, then here&#8217;s some code:</p>
<p><pre name="code" class="c">
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
  // Use the new 3.2 style API
  moviePlayer.controlStyle = MPMovieControlStyleNone;
  moviePlayer.shouldAutoplay = YES;
  [self.view addSubview:moviePlayer.view];
  [moviePlayer setFullscreen:YES animated:YES];
} else {
  // Use the old 2.0 style API
  moviePlayer.movieControlMode = MPMovieControlModeHidden;
  [moviePlayer play];
}
</pre>
<pre name="code" class="c">
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
  MPMoviePlayerController *moviePlayer = [notification object];
  [[NSNotificationCenter defaultCenter] removeObserver:self
                                                  name:MPMoviePlayerPlaybackDidFinishNotification
                                                object:moviePlayer];

  // If the moviePlayer.view was added to the view, it needs to be removed
  if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    [moviePlayer.view removeFromSuperview];
  }

  [moviePlayer release];
}
</pre>
</p>
<p> </p>
<p>Recently I&#8217;ve been doing some development using <a href="http://www.cocos2d-iphone.org/">cocos2d</a> and I had to make some small changes to the code. (If your app is in portrait mode, then you may not have to do the rotation transform shown below.)</p>
<p><pre name="code" class="c">
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
  // Use the new 3.2 style API
  moviePlayer.controlStyle = MPMovieControlStyleNone;
  moviePlayer.shouldAutoplay = YES;
  // This does blows up in cocos2d, so we'll resize manually
  // [moviePlayer setFullscreen:YES animated:YES];
  [moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];
  CGSize winSize = [[CCDirector sharedDirector] winSize];
  moviePlayer.view.frame = CGRectMake(0, 0, winSize.height, winSize.width);// width and height are swapped after rotation
  [[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view];
} else {
  // Use the old 2.0 style API
  moviePlayer.movieControlMode = MPMovieControlModeHidden;
  [moviePlayer play];
}
</pre></p>
<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/play-video-with-mpmovieplayercontroller-in-ios-3-0-and-3-24-0">Play Video with MPMoviePlayerController in iOS 3.0 and 3.2/4.0</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneIncubator.com/blog/audio-video/play-video-with-mpmovieplayercontroller-in-ios-3-0-and-3-24-0/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Play YouTube Videos Within an Application</title>
		<link>http://iPhoneIncubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application</link>
		<comments>http://iPhoneIncubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application#comments</comments>
		<pubDate>Tue, 03 Mar 2009 16:33:15 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Audio & Video]]></category>
		<category><![CDATA[UIWebView]]></category>

		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=94</guid>
		<description><![CDATA[Happy Square Root Day!
Playing a video (of a supported file format) on the iPhone is very easy using the MPMoviePlayerController class. You just create an instance of the class and initialize it with the URL of the video. The controller plays the video in full screen mode and returns back to your application when it&#8217;s [...]<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application">How To Play YouTube Videos Within an Application</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Happy <a href="http://en.wikipedia.org/wiki/Square_root_day">Square Root Day</a>!</p>
<p>Playing a video (of a supported file format) on the iPhone is very easy using the MPMoviePlayerController class. You just create an instance of the class and initialize it with the URL of the video. The controller plays the video in full screen mode and returns back to your application when it&#8217;s done.</p>
<p>However, if the URL of the video is recognized by the iPhone as a YouTube URL then the Apple URL Scheme mechanism kicks in and launches the YouTube app. In this scenario control will not return to your app after the video has played. (The behavior is equivalent of calling [UIApplication openURL:] with the video URL.)</p>
<p>One workaround is to use a UIWebView and load it with the video URL. The drawback with this approach is that the user will see the rather ugly YouTube mobile web site and has to find and tap on the link of the video to play it.</p>
<p><img src="http://iPhoneIncubator.com/blog/wp-content/uploads/2009/03/youtubemobile.png" alt="YouTube Mobile" width="320" height="480" /></p>
<p> </p>
<p>Another, visually more appealing, option is to create a small UIWebView on your screen and load it with the YouTube embed code. The result is a small button like image that shows the YouTube play button above a screen image from the video. When the user taps the image, the iPhone video player opens in full screen mode as usual, and when the video is done control is returned back to this screen.</p>
<p><img src="http://iPhoneIncubator.com/blog/wp-content/uploads/2009/03/youtubeembeddedvideo.png" alt="YouTube Video Embedded in iPhone App" width="320" height="480" /></p>
<p> </p>
<p>Here&#8217;s the code:</p>
<pre name="code" class="c">- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
 NSString* embedHTML = @"\
    &lt;html&gt;&lt;head&gt;\
 &lt;style type=\"text/css\"&gt;\
 body {\
 background-color: transparent;\
 color: white;\
 }\
 &lt;/style&gt;\
 &lt;/head&gt;&lt;body style=\"margin:0\"&gt;\
    &lt;embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
 width=\"%0.0f\" height=\"%0.0f\"&gt;&lt;/embed&gt;\
    &lt;/body&gt;&lt;/html&gt;";
 NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
 if(videoView == nil) {
 	videoView = [[UIWebView alloc] initWithFrame:frame];
 	[self.view addSubview:videoView];
 }
 [videoView loadHTMLString:html baseURL:nil];
}</pre>
<p><em>Tip of the hat to </em><a href="https://devforums.apple.com/message/25053"><em>joehewitt</em></a><em>.</em></p>
<p><strong>Update:</strong> A complete working Xcode project has been posted <a href="http://iPhoneIncubator.com/blog/windows-views/360idev-iphone-developers-conference-presentation">here</a>.</p>
<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application">How To Play YouTube Videos Within an Application</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneIncubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application/feed</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>A Simple Way To Play MP3 Audio Files</title>
		<link>http://iPhoneIncubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files</link>
		<comments>http://iPhoneIncubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files#comments</comments>
		<pubDate>Thu, 19 Feb 2009 22:10:16 +0000</pubDate>
		<dc:creator>Jess</dc:creator>
				<category><![CDATA[Audio & Video]]></category>
		<category><![CDATA[NSURLRequest]]></category>
		<category><![CDATA[UIWebView]]></category>

		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=92</guid>
		<description><![CDATA[The iPhone media player is very easy to use, but it only works in landscape mode and it seems designed more for video than audio.  If you wish to play an onboard MP3 or an MP3 file as it&#8217;s downloading (not really streaming, but progressive download) you have another option!  Use the audio player embedded with [...]<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files">A Simple Way To Play MP3 Audio Files</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The iPhone media player is very easy to use, but it only works in landscape mode and it seems designed more for video than audio.  If you wish to play an onboard MP3 or an MP3 file as it&#8217;s downloading (not really streaming, but progressive download) you have another option!  Use the audio player embedded with Safari running in a UIWebView.</p>
<p>Here&#8217;s how to do it without having to create a special window for it.  Instantiate an UIWebView object using a 1&#215;1 pixel sized frame (here self.playerView is an instance variable on my controller and is NOT added as a subview)</p>
<pre name="code" class="c">UIWebView *webView = [[UIWebView alloc] initWithFrame: CGRectMake(0.0, 0.0, 1.0, 1.0)];
webView.delegate = self;
self.playerView = webView;
[webView release];</pre>
<p>Then load your NSURLRequest.</p>
<pre name="code" class="c">NSURLRequest *request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: myMP3URL] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: myTimeoutValue];
[self.playerView loadRequest: request];
[request release];</pre>
<p>That&#8217;s it!  When the request starts downloading the player with full controls will launch and your screen will look the the picture below.</p>
<p><img src="http://iPhoneIncubator.com/blog/wp-content/uploads/2009/02/audioplayerinwebview.png" alt="Audio player launched from a UIWebView" width="414" height="770" /></p>
<p>Check out the web view delegate methods to learn more about controlling the behavior of the web view to suite your specific needs.</p>
<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files">A Simple Way To Play MP3 Audio Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneIncubator.com/blog/audio-video/simple-way-to-play-mp3-audio-files/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>How To Play Audio With The iPhone SDK</title>
		<link>http://iPhoneIncubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk</link>
		<comments>http://iPhoneIncubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk#comments</comments>
		<pubDate>Mon, 10 Mar 2008 17:19:53 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Audio & Video]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[audio]]></category>

		<guid isPermaLink="false">http://iPhoneIncubator.com/blog/?p=6</guid>
		<description><![CDATA[Playing audio with the iPhone Software Development Kit (SDK) should be easy, right? After all this is an iPod &#8211; the music jukebox.
After some fruitless experiments I decided to take a shortcut and use the audio support classes in the JigSaw sample application. Copy the entire AudioSupport directory from the JigSaw project. It should contain [...]<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk">How To Play Audio With The iPhone SDK</a></p>
]]></description>
			<content:encoded><![CDATA[<p><span>Playing audio with the iPhone Software Development Kit (SDK) should be easy, right? After all this is an iPod &#8211; <em>the</em> music jukebox.</span></p>
<p><span>After some fruitless experiments I decided to take a shortcut and use the audio support classes in the JigSaw sample application. Copy the entire AudioSupport directory from the JigSaw project. It should contain the following files:</span></p>
<ul>
<li>Audio_Internal.h</li>
<li>AudioFX.h</li>
<li>AudioFX.m</li>
</ul>
<p> </p>
<p><span>You can copy and add the files to your project in Xcode in one fell swoop:</span></p>
<ol>
<li>Command-click on the Classes folder.</li>
<li>Select Add &gt; Existing Files</li>
<li>Navigate to the JigSaw sample project and select the AudioSupport directory. Click Add.</li>
<li>Check the “Copy items” checkbox. Click Add.</li>
<li>Now you should have a new folder called AudioSupport under Classes in your project.</li>
</ol>
<p> </p>
<p>You also need to add the AudioToolbox framework</p>
<ol>
<li>Command-click on the Linked Frameworks folder inside the Frameworks folder in your Xcode project.</li>
<li>Select Add &gt; Existing Frameworks</li>
<li>Navigate to the AudioToolbox.framework folder. Click Add. If you can’t immediately find this file, try this location: /Developer/Platforms/AspenSimulator.platform/Developer/SDKs/AspenSimulator1.2.sdk/System/Library/Frameworks</li>
<li>Click Add again.</li>
</ol>
<p> </p>
<p><span>Your project should now build without any errors.</span></p>
<p> </p>
<p><span>Of course you need an audio file to play. The iPhone is capable of playing a large number of audio file formats [link to dev center]. But to start with I recommend that you select a file that is known to work. Let’s go back to the JigSaw sample application again.</span></p>
<ol>
<li>Command-click on the Resources folder in your Xcode project.</li>
<li>Select Add &gt; Existing Files</li>
<li>Navigate to the Sounds directory in the JigSaw project. Select one of the sound files. (They have the .caf filename extension.)</li>
<li>Click Add.</li>
<li>Check the “Copy items” checkbox. Click Add.</li>
</ol>
<p> </p>
<p><span>Now we need some code to play the audio file.</span></p>
<p> </p>
<p><span>Add this at the top of your file:</span></p>
<pre class="c">#import "AudioFX.h"</pre>
<p> </p>
<p><span>Then use this code where you want to play the audio file:</span></p>
<pre class="c">AudioFX *audio = [[AudioFX alloc] initWithPath:@"Completed.caf"];
[audio play];</pre>
<p> </p>
<p>“Completed.caf” is the name of the audio file that you added as a resource to your project.</p>
<p>The above code is simple and to the point, but it has a few problems. One obvious one is that an AudioFX object is allocated but never released. Adding [audio release]; just after [audio play]; will blow up because the play method is asynchronous: it returns immediately. And releasing the AudioFX object while it’s playing is not very nice.</p>
<p>Another problem is that the code allocates a new AudioFX object each time that audio file is played. If you are going to play the same audio more than once, you should create the AudioFX object once in the init method and use it throughout the life of the app. You can then safely release the object in the dealloc method. This also solves problem number one.</p>
<p> </p>
<p>Here’s the final code, used in a UIController:</p>
<pre class="c">- (id)init
{
  if (self = [super init]) {
    // Initialize your view controller.
    self.title = @"Play Audio"; 

audio = [[AudioFX alloc] initWithPath:@"Completed.caf"];
  }
  return self;
}

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

- (void)viewDidAppear:(BOOL)animated
{
  [audio play];
}

 </pre>
<p><span>UPDATE: This code example is outdated. Please see the SoundEffect class used in the BubbleLevel, GLPaint and Metronome projects instead. The interface is very similar, but instead of initWithPath use initWithContentsOfFile.</span></p>
<p>Post from <a href="http://iPhoneIncubator.com/blog">iPhone Development Blog</a> Copyright &copy; 2009 Nick Dalton - <a href="http://iPhoneIncubator.com/blog/portfolio">iPhone Developer</a><br/><br/><a href="http://iPhoneIncubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk">How To Play Audio With The iPhone SDK</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneIncubator.com/blog/tutorial/how-to-play-audio-with-the-iphone-sdk/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
