<?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 Tutorial &#124; iPhone iOS4 iPad SDK Development &#38; Programming Blog &#187; Music Play</title>
	<atom:link href="http://www.edumobile.org/iphone/tag/music-play/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edumobile.org/iphone</link>
	<description></description>
	<lastBuildDate>Wed, 25 Jan 2012 09:42:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Music Play in iPhoneOS4</title>
		<link>http://www.edumobile.org/iphone/iphone-programming-tutorials/music-play-in-iphoneos4/</link>
		<comments>http://www.edumobile.org/iphone/iphone-programming-tutorials/music-play-in-iphoneos4/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 13:27:15 +0000</pubDate>
		<dc:creator>Sushant</dc:creator>
				<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone tutorials]]></category>
		<category><![CDATA[Music Play]]></category>
		<category><![CDATA[Music Play in iPhoneOS4]]></category>

		<guid isPermaLink="false">http://www.edumobile.org/iphone/?p=2517</guid>
		<description><![CDATA[This is the very simple example . In this application we will see how to play music in iPhone oS4.]]></description>
			<content:encoded><![CDATA[<p>This is the very simple example . In this application we will see how to play music in iPhone oS4.</p>
<p><strong>Step 1:</strong> Create a new project in Xcode using View base application. Give the application name “MusicPlay_OS4”.</p>
<p><strong>Step 2:</strong> Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.</p>
<p><strong>Step 3:</strong> xpand classes and notice Interface Builder created the MusicPlay_OS4ViewController class for you. Expand Resources and notice the template generated a separate nib, MusicPlay_OS4ViewController.xib, for the “MusicPlay_OS4”.</p>
<p><strong>Step 4:</strong> We need to add sound files in the Resources folder. Give the name of the sound files “music.mp3”,”sound.aif”. And also add a backgroung image.</p>
<p><strong>Step 5:</strong> We need to add also two frameworks.So select Frameworks -&gt; Add -&gt; Existing Framework -&gt; then select AVFoundation.framework and AudioToolbox.framework.</p>
<p><strong>Step 6:</strong> In the MusicPlay_OS4ViewController.h file, we have  importAVFoundation.framework and AudioToolbox.framework. Create a instance of  AVAudioPlayer and UIButton class and create two IBAction method. So make the following changes in the file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="co1">#import &lt;UIKit/UIKit.h&gt;</span><br />
<span class="co1">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span><br />
<span class="co1">#import &lt;AVFoundation/AVFoundation.h&gt;</span></p>
<p><span class="kw1">@interface</span> MusicPlay_OS4ViewController <span class="sy0">:</span> UIViewController <br />
&lt;AVAudioPlayerDelegate&gt;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; SystemSoundID systemSoundID;<br />
&nbsp; &nbsp; &nbsp; &nbsp; AVAudioPlayer <span class="sy0">*</span>player;<br />
&nbsp; &nbsp; &nbsp; &nbsp; UIButton <span class="sy0">*</span>StartStopSound;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span> IBOutlet AVAudioPlayer <span class="sy0">*</span>player;<br />
<span class="kw1">@property</span> <span class="br0">&#40;</span>nonatomic, retain<span class="br0">&#41;</span> IBOutlet UIButton <span class="sy0">*</span>StartStopSound;</p>
<p>
<span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span> playSound<span class="sy0">:</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span> sender;<br />
<span class="sy0">-</span> <span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span> playSong<span class="sy0">:</span> <span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span> sender;</div>
</div>
<p><strong>Step 7:</strong> Double click the MusicPlay_OS4ViewController.xib file and open it to the Interface Builder. First drag the Image View from the library and place it to the View window. Select the image view and bring up Attribute Inspector, select the background image. Drag two Round Rect button from the library and place it to the view window.Give the name of the button &#8220;Play Sound &#8220;and &#8220;Play Song&#8221; Connect File&#8217;s Owner icon to the View icon and select View. Drag File&#8217;s Owner icon to the Play Song button and select StartStopSound. Select the Play Sound button and bring up Connection Inspector and drag from the Touch Up Inside to the File&#8217;s Owner icon, select playSound: action. Do the same thing with PlaySong button and select the playSong: action. Now save the MusicPlay_OS4ViewController.xib file, close it and go back to the Xcode.</p>
<p><strong>Step 8: </strong>Open the MusicPlay_OS4ViewController.m file and make the following changes in the file.</p>
<div class="codesnip-container" >
<div class="objc codesnip" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#41;</span>viewDidLoad <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span class="br0">&#40;</span><span class="co3">@</span><span class="st0">&quot;InView did load&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; AudioServicesCreateSystemSoundID<span class="br0">&#40;</span><span class="br0">&#40;</span>CFURLRef<span class="br0">&#41;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span class="kw5">NSURL</span></a> fileURLWithPath<span class="sy0">:</span><span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span class="kw5">NSBundle</span></a> mainBundle<span class="br0">&#93;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pathForResource<span class="sy0">:</span> <span class="co3">@</span><span class="st0">&quot;sound&quot;</span> ofType<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;aif&quot;</span><span class="br0">&#93;</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sy0">&amp;</span>systemSoundID<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; player <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>AVAudioPlayer alloc<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initWithContentsOfURL<span class="sy0">:</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span class="kw5">NSURL</span></a> fileURLWithPath<span class="sy0">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span class="kw5">NSBundle</span></a> mainBundle<span class="br0">&#93;</span> pathForResource<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;music&quot;</span> ofType<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;mp3&quot;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error<span class="sy0">:</span><span class="kw2">nil</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>player prepareToPlay<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></p>
<p>
<span class="sy0">-</span><span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span> playSound<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span> sender <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog<span class="br0">&#40;</span><span class="co3">@</span><span class="st0">&quot;In Sample&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; AudioServicesPlaySystemSound<span class="br0">&#40;</span>systemSoundID<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">-</span><span class="br0">&#40;</span>IBAction<span class="br0">&#41;</span> playSong<span class="sy0">:</span><span class="br0">&#40;</span><span class="kw4">id</span><span class="br0">&#41;</span> sender <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> stringWithFormat<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;%@&quot;</span>, <span class="br0">&#91;</span>StartStopSound titleForState<span class="sy0">:</span>UIControlStateNormal<span class="br0">&#93;</span><span class="br0">&#93;</span> isEqualToString<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Play Song&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>player play<span class="br0">&#93;</span>;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>StartStopSound setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Stop Song&quot;</span> forState<span class="sy0">:</span>UIControlStateNormal<span class="br0">&#93;</span>; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>player stop<span class="br0">&#93;</span>;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>StartStopSound setTitle<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Play Song&quot;</span> forState<span class="sy0">:</span>UIControlStateNormal<span class="br0">&#93;</span>; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Step 9:</strong> Now compile and run the application in the Simulator.</p>
<p><a href="http://www.edumobile.org/iphone/wp-content/uploads/2010/07/1-76.jpg"><img class="alignnone size-medium wp-image-2518" title="1-7" src="http://www.edumobile.org/iphone/wp-content/uploads/2010/07/1-76-160x300.jpg" alt="" width="160" height="300" /></a></p>
<p>You can <strong>Download Source</strong> Code from here <a href='http://www.edumobile.org/iphone/wp-content/uploads/2010/07/MusicPlay_OS4.zip'>MusicPlay_OS4</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.edumobile.org/iphone/iphone-programming-tutorials/music-play-in-iphoneos4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

