<?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>FatBasturd &#187; iPhone Development</title>
	<atom:link href="http://www.fatbasturd.co.uk/tag/iphone-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fatbasturd.co.uk</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 20 Aug 2009 10:58:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>iPhone App Development Tips &#8211; Playing multiple sounds using AVAudioPlayer</title>
		<link>http://www.fatbasturd.co.uk/playing-multiple-sounds-using-avaudioplayer/</link>
		<comments>http://www.fatbasturd.co.uk/playing-multiple-sounds-using-avaudioplayer/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 08:54:38 +0000</pubDate>
		<dc:creator>FatBasturd</dc:creator>
				<category><![CDATA[Gaming, Gadgets & Technology]]></category>
		<category><![CDATA[iPhone App Development]]></category>
		<category><![CDATA[Apple Mac]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://www.fatbasturd.co.uk/?p=176</guid>
		<description><![CDATA[During my journey through App development for the iPhone I will be posting some code that initially gave me a headache and I either managed to solve myself or my good friend Hardy from Catamount Software helped me with.
The first block of code is something that i was struggling with for my latest app called iAnnoyance [...]]]></description>
			<content:encoded><![CDATA[<p>During my journey through App development for the iPhone I will be posting some code that initially gave me a headache and I either managed to solve myself or my good friend Hardy from Catamount Software helped me with.</p>
<p>The first block of code is something that i was struggling with for my latest app called iAnnoyance which is an annoying sounds app. The result i wanted was to have multiple sounds ready to play (.mp3) but each one if triggered must be stopped upon triggering another. In laymens terms &#8220;I wanted to stop one sound when another was playing&#8221;.</p>
<p>The avenue I decided to go up was the AVAudioPlayer route which I chose for the functionality such as looping and play, stop, pause functions.</p>
<p>So rather than create a seperate player for each sound it was decided that the app could use a single player that would watch the button presses and work out which sound needed to be loaded on the fly. This was achieved using the following code in my MainViewController.m</p>
<pre>-(void)playSound:(NSString*)soundName
{
	if (self.currentSound.isPlaying)
	{
		[self.currentSound stop];
	}

	if (![self.lastSoundPlayed isEqual:soundName])
	{
		NSString *soundPath = [[NSBundle mainBundle]
                   pathForResource:soundName ofType:@"mp3"];
		AVAudioPlayer *player =  [[AVAudioPlayer alloc]
                  initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
		[player setDelegate:self];
		[player prepareToPlay];
		[player play];

		self.currentSound = player;
		[player release];

		self.lastSoundPlayed = soundName;
	}
}</pre>
<p>Each button would then look like so:-<span id="more-176"></span></p>
<pre>-(IBAction)cryBaby:(id)sender{

	[self playSound:@"Cry"];

}

-(IBAction)snoringMan:(id)sender{

	[self playSound:@"Snore"];

}</pre>
<p>Then make sure you release the sounds and free up memory at the end of the file using the following:-</p>
<pre>[currentSound release];
[lastSoundPlayed release];</pre>
<p>currentSound &amp; lastSoundPlayed were also added to the top of the file using the following:-</p>
<pre>@synthesize currentSound;
@synthesize lastSoundPlayed;</pre>
<p>My MainViewController.h would then have the following added:-</p>
<pre>#import &lt;AVFoundation/AVAudioPlayer.h&gt;</pre>
<p>And the following:-</p>
<pre>@interface MainViewController : UIViewController &lt;AVAudioPlayerDelegate&gt; {
                        AVAudioPlayer *currentSound;
                        NSString *lastSoundPlayed;
                        }
@property (retain) AVAudioPlayer *currentSound;
@property (retain) NSString *lastSoundPlayed;</pre>
<p>Using this code you can set up your buttons to listen for your button presses and stop or start a sound respectively. Hopefully this will help you in your App development and as usual, if you have any questions feel free to post them below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fatbasturd.co.uk/playing-multiple-sounds-using-avaudioplayer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
