<?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 developer:tips]; &#187; Audio</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/audio/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 11 Mar 2010 14:32:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Playing Sounds/Audio &#8211; AVAudioPlayer</title>
		<link>http://iPhoneDeveloperTips.com/audio/playing-soundsaudio-avaudioplayer.html</link>
		<comments>http://iPhoneDeveloperTips.com/audio/playing-soundsaudio-avaudioplayer.html#comments</comments>
		<pubDate>Mon, 07 Dec 2009 10:05:57 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Audio]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4775</guid>
		<description><![CDATA[I previously wrote about using Audio Session Services which offers a quick and easy approach for playing short sounds. However, there are a number of limitations, including no control over the volume, playing time is limited to 30 seconds and the only file types supported are AIFF, WAV and CAF.
Sounds with AVAudioPlayer
AVAudioPlayer offers a much [...]]]></description>
			<content:encoded><![CDATA[<p>I previously wrote about <a  target="_blank"  href="http://iphonedevelopertips.com/audio/playing-short-sounds-audio-session-services.html">using Audio Session Services</a> which offers a quick and easy approach for playing short sounds. However, there are a number of limitations, including no control over the volume, playing time is limited to 30 seconds and the only file types supported are AIFF, WAV and CAF.</p>
<h5>Sounds with AVAudioPlayer</h5>
<p><strong>AVAudioPlayer</strong> offers a much wider range of capabilites, including pausing playback, adjusting the volume, tinkering with the playback time (setting and detecting) as well as features for monitoring audio peaks and averages. </p>
<p>The code below shows the basics for setting up a player and playing a sound:<br />
<span id="more-4775"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
AVAudioPlayer  <span style="color: #002200;">*</span>player;
<span style="color: #400080;">NSString</span>       <span style="color: #002200;">*</span>path;
<span style="color: #400080;">NSError</span>        <span style="color: #002200;">*</span>error;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Path the audio file</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;sound-file&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mp3&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// If we can access the file...</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>path<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;">// Setup the player</span>
  player <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AVAudioPlayer alloc<span style="color: #002200;">&#93;</span> initWithContentsOfURL<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> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the volume (range is 0 to 1)</span>
  player.volume <span style="color: #002200;">=</span> 0.4f;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// To minimize lag time before start of output, preload buffers      </span>
  <span style="color: #002200;">&#91;</span>player prepareToPlay<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Play the sound once (set negative to loop)</span>
  <span style="color: #002200;">&#91;</span>player setNumberOfLoops<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>player play<span style="color: #002200;">&#93;</span>;    
&nbsp;
<span style="color: #002200;">&#125;</span>    
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// Cleanup</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>player <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>player.isPlaying <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>player stop<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>player release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/audio/playing-soundsaudio-avaudioplayer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silent Mode Switch and Playing Sounds</title>
		<link>http://iPhoneDeveloperTips.com/audio/silent-mode-switch-and-playing-sounds.html</link>
		<comments>http://iPhoneDeveloperTips.com/audio/silent-mode-switch-and-playing-sounds.html#comments</comments>
		<pubDate>Wed, 01 Jul 2009 02:50:53 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Audio]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2469</guid>
		<description><![CDATA[In a previous tip, Playing Short Sounds, I wrote an example to show how you can play a short sound, up to 30 seconds, by calling one of the C-based functions in the Audio Session Services library. 
There is one aspect of this that can be troublesome &#8211; by default, playing an Audio Session sound [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous tip, <a target="_blank" href="http://iphonedevelopertips.com/audio/playing-short-sounds-audio-session-services.html">Playing Short Sounds</a>, I wrote an example to show how you can play a short sound, up to 30 seconds, by calling one of the C-based functions in the Audio Session Services library. </p>
<p>There is one aspect of this that can be troublesome &#8211; by default, playing an Audio Session sound will ~not~ respect the setting of the mute switch on the iPhone. In other words, if you make a call to play a sound and the silent (hardware) switch on the iPhone is set to silent, you&#8217;ll still hear the sound.<br />
<span id="more-2469"></span>  </p>
<p>To respect the setting of the silent switch, add this code above any calls to play a sound:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Choose a category identifier for audio sessions</span>
UInt32 sessionCategory <span style="color: #002200;">=</span> kAudioSessionCategory_SoloAmbientSound;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the session property</span>
AudioSessionSetProperty<span style="color: #002200;">&#40;</span>kAudioSessionProperty_AudioCategory,
    <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>sessionCategory<span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&amp;</span>sessionCategory<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>There are a number of pre-defined session categories, many will have the same end result as the kAudioSessionCategory_SoloAmbientSound that I opted to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/audio/silent-mode-switch-and-playing-sounds.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Playing Sounds/Audio &#8211; Audio Session Services</title>
		<link>http://iPhoneDeveloperTips.com/audio/playing-short-sounds-audio-session-services.html</link>
		<comments>http://iPhoneDeveloperTips.com/audio/playing-short-sounds-audio-session-services.html#comments</comments>
		<pubDate>Sat, 27 Jun 2009 05:09:33 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Audio]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2424</guid>
		<description><![CDATA[If you need to play short sounds, less than 30 seconds, Audio Session Services are your friend. Here is a snippet of code to play a wav file:

SystemSoundID soundID;
NSString *path = &#91;&#91;NSBundle mainBundle&#93;
   pathForResource:@&#34;RapidFire&#34; ofType:@&#34;wav&#34;&#93;;    
&#160;
AudioServicesCreateSystemSoundID&#40;&#40;CFURLRef&#41;&#91;NSURL fileURLWithPath:path&#93;,&#38;soundID&#41;;
AudioServicesPlaySystemSound &#40;soundID&#41;;


For this to work, you will need to import &#60;AudioToolbox/AudioToolbox.h&#62; header file, and [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to play short sounds, less than 30 seconds, Audio Session Services are your friend. Here is a snippet of code to play a wav file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">SystemSoundID soundID;
<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;RapidFire&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wav&quot;</span><span style="color: #002200;">&#93;</span>;    
&nbsp;
AudioServicesCreateSystemSoundID<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</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>,<span style="color: #002200;">&amp;</span>soundID<span style="color: #002200;">&#41;</span>;
AudioServicesPlaySystemSound <span style="color: #002200;">&#40;</span>soundID<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><span id="more-2424"></span></p>
<p>For this to work, you will need to import &lt;AudioToolbox/AudioToolbox.h&gt; header file, and also add the AudioToolbox.framework to your project.</p>
<p>For those interested in a little background information, Audio Session Services is a set of functions written in C. There are a few subtleties that give this away. Here is the definition for the AudioServicesCreateSystemSoundID function:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">OSStatus AudioServicesCreateSystemSoundID <span style="color: #002200;">&#40;</span>
   CFURLRef       inFileURL, 
   SystemSoundID  <span style="color: #002200;">*</span>outSystemSoundID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The giveaway is how we call this function:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">AudioServicesCreateSystemSoundID<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</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>,<span style="color: #002200;">&amp;</span>soundID<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Notice the parameter outSystemSoundID, which is a pointer to SystemSoundID, a 32 bit unsigned integer. To pass this value as a pointer in C, you pass the address of the variable, which is done using &#038;soundID.</p>
<p>More tips on Audio Session Services in the works&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/audio/playing-short-sounds-audio-session-services.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
