<?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>Blog d&#039;un développeur multi-support &#187; son</title>
	<atom:link href="http://www.oni-ecchi.info/blog/tag/son/feed" rel="self" type="application/rss+xml" />
	<link>http://www.oni-ecchi.info/blog</link>
	<description>[DIM] pour les intimes :)</description>
	<lastBuildDate>Mon, 06 Feb 2012 18:51:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>iPhone &amp; AVAudioPlayer</title>
		<link>http://www.oni-ecchi.info/blog/iphone/iphone-avaudioplayer.html</link>
		<comments>http://www.oni-ecchi.info/blog/iphone/iphone-avaudioplayer.html#comments</comments>
		<pubDate>Wed, 18 Mar 2009 16:11:19 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[avancer]]></category>
		<category><![CDATA[son]]></category>
		<category><![CDATA[tutoriel]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=312</guid>
		<description><![CDATA[Aujourd&#8217;hui, j&#8217;ai bataillé pour lancer un mp3 au lancement d&#8217;une application. Tellement bataillé que je vais mettre ici le code qui va bien pour que je puisse le retrouver rapidement :) Donc peu d&#8217;explication, ceci n&#8217;est pas un tutorial. Inclure le framework AudioToolBox &#38; AVFoundation Faire les imports suivants : #import &#38;lt;AVFoundation/AVFoundation.h&#38;gt; #import &#38;lt;AudioToolbox/AudioToolbox.h&#38;gt; Inclure [...]]]></description>
			<content:encoded><![CDATA[<p>Aujourd&#8217;hui, j&#8217;ai bataillé pour lancer un mp3 au lancement d&#8217;une application. Tellement bataillé que je vais mettre ici le code qui va bien pour que je puisse le retrouver rapidement :) Donc peu d&#8217;explication, ceci n&#8217;est pas un tutorial.</p>
<ol>
<li>Inclure le framework AudioToolBox &amp; AVFoundation</li>
<li>Faire les imports suivants : #import &amp;lt;AVFoundation/AVFoundation.h&amp;gt; #import &amp;lt;AudioToolbox/AudioToolbox.h&amp;gt;</li>
<li>Inclure le fichier à jouer dans le projet dans le dossier &laquo;&nbsp;ressources&nbsp;&raquo;</li>
<li>Déclarer une propriété de classe AVAudioPlayer * audioPlayer;</li>
</ol>
<p>Et voici le code qui va bien :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p312code2'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3122"><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
36
</pre></td><td class="code" id="p312code2"><pre class="c" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Initialise un contexte Audio</span>
	AudioSessionInitialize <span style="color: #009900;">&#40;</span>
							NULL<span style="color: #339933;">,</span>                          <span style="color: #666666; font-style: italic;">// 'NULL' to use the default (main) run loop</span>
							NULL<span style="color: #339933;">,</span>                          <span style="color: #666666; font-style: italic;">// 'NULL' to use the default run loop mode</span>
							NULL<span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// a reference to your interruption callback</span>
							self                       <span style="color: #666666; font-style: italic;">// data to pass to your interruption listener callback</span>
							<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Definit quel type de son on va joué</span>
	UInt32 sessionCategory <span style="color: #339933;">=</span> kAudioSessionCategory_MediaPlayback<span style="color: #339933;">;</span>
	AudioSessionSetProperty <span style="color: #009900;">&#40;</span>
							 kAudioSessionProperty_AudioCategory<span style="color: #339933;">,</span>
							 <span style="color: #993333;">sizeof</span> <span style="color: #009900;">&#40;</span>sessionCategory<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
							 <span style="color: #339933;">&amp;</span><span style="color: #339933;">#038;sessionCategory</span>
							 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Définit quel fichier on utilise pour le son</span>
	NSData <span style="color: #339933;">*</span>soundFileData<span style="color: #339933;">;</span>
	soundFileData <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSData dataWithContentsOfURL<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>NSURL fileURLWithPath<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>NSBundle mainBundle<span style="color: #009900;">&#93;</span> pathForResource<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;music.mp3&quot;</span> ofType<span style="color: #339933;">:</span>NULL<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialise notre lecteur avec les données du fichiers son à jouer</span>
	audioPlayer <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>AVAudioPlayer alloc<span style="color: #009900;">&#93;</span> initWithData<span style="color: #339933;">:</span>soundFileData error<span style="color: #339933;">:</span>NULL<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Precharge le son</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>audioPlayer prepareToPlay<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		NSLog<span style="color: #009900;">&#40;</span>@<span style="color: #ff0000;">&quot;myAudioPlayer:prepareToPlay returned FALSE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Définit le delegate sur la classe courante</span>
	audioPlayer.<span style="color: #202020;">delegate</span> <span style="color: #339933;">=</span> self<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Définit le volume</span>
	<span style="color: #009900;">&#91;</span>audioPlayer setVolume<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Joue le son !</span>
	<span style="color: #009900;">&#91;</span>audioPlayer play<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Voilà, la seul différence avec les codes que l&#8217;on peut trouver un peu partout est l&#8217;ajout du Session Audio. Si je l&#8217;initialise pas le son ne fonctionne que sur le simulateur et pas sur le mobile. Je ne sais pas trop pourquoi.</p>
<p>Merci à <a href="http://www.linkedin.com/in/fpillet" rel="nofollow">fpillet</a> du channel irc #iphonedev sur freenode pour son aide.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/iphone/iphone-avaudioplayer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

