<span id="Label3"></p><span style="color: #0000ff; font-size: 16px;"><span style="color: #0000ff; font-size: 16px;">several ways to play audio on IOS</span></span><p><p><span style="font-size: 15px;">The IPhone OS mainly offers several ways to play audio:</span></p></p> <ul> <ul> <li><span style="font-size: 15px;">System Sound Services</span></li> <li><span style="font-size: 15px;">Avaudioplayer class</span></li> <li><span style="font-size: 15px;">Audio Queue Services</span></li> <li><span style="font-size: 15px;">OpenAL</span></li> </ul> </ul><p><p><span style="font-size: 15px; color: #ff6600;">1. System Sound Services</span></p></p><p><p><span style="font-size: 15px;">System Sound Services is the lowest and simplest voice playback service, Call Audioservicesplaysystemsound This method can play some simple audio files, using this method is only suitable for playing some very small hints or warning tones, Because it has a lot of limitations:</span></p></p><p><p><span style="font-size: 15px;"><em>Sound length less than 30 seconds<br>In linear PCM or IMA4 (ima/adpcm) format<br>Package as. caf,. aif, or. wav files<br>Cannot control the progress of playback<br>Play sound immediately after calling method<br>No loop playback and stereo control</em></span></p></p><p><p><span style="font-size: 15px;">In addition, it can call the System's vibration function, The method is also very simple. The specific code can refer to the official example Syssound<br>, but the official example only has some simple usage, from the document we find that we can add CallBack function for audio playback through the audioservicesaddsystemsoundcompletion method, with CallBack function we can solve many problems, For example, you can overcome the problem that System sound Services does not natively support looping PLAYBACK.</span></p></p><p><p><span style="font-size: 15px; color: #ff6600;">2. Avaudioplayer class</span><br><span style="font-size: 15px;">Avaudioplayer is a class defined in avfoundation.framework, so the use is to first introduce avfoundation.framework in the Project. We can think of Avaudioplayer as an advanced player that supports a wide range of audio formats, mainly in the following formats:</span></p></p><p><p><span style="font-size: 15px;"><em>AAC<br>AMR (adaptivemulti-rate, Aformatforspeech)<br>ALAC (applelossless)<br>ILBC (internetlowbitratecodec, Anotherformatforspeech)<br>IMA4 (ima/adpcm)<br>LINEARPCM (uncompressed)<br>Μ-lawanda-law<br>MP3 (mpeg-1audiolayer3</em></span></p></p><p><p><span style="font-size: 15px;">Avaudioplayer can play audio files of any length, support looping, play multiple audio files simultaneously, control playback progress and start playback from any point in the audio file, and more advanced features can refer to Avaudioplayer's Documentation. To play a file using Avaudioplayer objects, you only need to specify an audio file for it and set a delegate object that implements the Avaudioplayerdelegate Protocol.</span></p></p><p><p><span style="font-size: 15px;">As soon as the Numberofloops property of Avaudioplayer is set to a negative number, the audio file will loop until the Stop method is Called.</span></p></p><p><p><span style="font-size: 15px;">Avaudioplayer also supports Callback, which is an optional delegate method for Avaudioplayerdelegate:</span></p></p><pre><pre><span style="font-size: 14px;"><strong>-(<span style="color: #0000ff;">void</span> ) audioplayerdidfinishplaying: (avaudioplayer *<span style="color: #000000;">) player successfully: (BOOL) flag { </span><span style="color: #0000ff;">if </span> (player = = Self. Player && flag = =<span style="color: #000000;"> YES) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">Playback Finish.</span> ) <span style="color: #800000;">"</span> <span style="color: #000000;"> ); } }</span></strong></span></pre></pre><p><p><span style="font-size: 15px;">In addition, you can control the playback, pause and stop of the Avaudioplayer object at any time, by judging the state of the object, call the play, pause and stop methods respectively:</span></p></p><pre><pre><span style="font-size: 14px;"><strong>-(ibaction) playorpause: (<span style="color: #0000ff;">ID</span> <span style="color: #000000;"> ) sender { </span><span style="color: #008000;">//</span><span style="color: #008000;"></span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (self. Player.playing) { [self. player pause]; </span> <span style="color: #008000;">//</span> else {<span style="color: #000000;"> [self. player play]; }</span> <span style="color: #0000ff;"></span> <span style="color: #008000;"></span> <span style="color: #000000;"> </span></strong></span></pre></pre><p><p><span style="font-size: 15px;">Although the Avaudioplayer can play a lot of formats, but in the actual development process is still best to use some non-compressed format, such as WAVE files, which can reduce the system Processing unit resource consumption, in order to better complete the Program's other Functions. In addition, when using Avaudioplayer to continuously play MP3 such compressed audio files, there may be a certain interval at the Junction.</span></p></p><p><p><span style="font-size: 15px; color: #ff6600;">3. Audio Queue Services</span></p></p><p><p><span style="font-size: 15px;">If the two audio playback solutions do not meet your needs, then I think you will definitely need to use audio Queue Services. With audio Queue Services, you can fully control the sound by playing it. For example, you can perform a fast/slow playback of audio when sound data is read from a file to a memory buffer and then played Back.</span></p></p><p><p><span style="font-size: 15px;">Because audio queue services is a lot more complex, Apple officials have organized it into a book, which can be found in the program examples of audio Queue Services Programming Guide and Speakhere.</span></p></p><p><p><span style="font-size: 15px; color: #ff6600;">4. OpenAL</span></p></p><p><p><span style="font-size: 15px;">OpenAL is a cross-platform, open-source audio processing interface, similar to the graphics-processing OpenGL, which provides a more optimized solution for audio Playback. It is ideal for developing game sounds and uses the same as other Platforms.</span></p></p><p><p><span style="font-size: 15px;">This article is mainly about avaudioplayer,</span></p></p><p><p><span style="font-size: 15px;">The Avaudioplayer class encapsulates the ability to play a single sound. Players can be initialized with Nsurl or nsdata, Note that Nsurl can not be a network URL and must be a local file url, because Avaudioplayer does not have the ability to play network Audio.</span></p></p><p><p><span style="font-size: 15px;">A avaudioplayer can only play one audio, and if you want to mix you can create multiple Avaudioplayer instances, each equivalent to a track on the mixer Board.</span></p></p><p><p><span style="font-size: 15px;">one, Create a player</span></p></p><pre><pre><span style="font-size: 14px;"><strong>Avaudioplayer *player = [[avaudioplayeralloc] initwithcontentsofurl:[nsurlfileurlwithpath:[[nsbundlemainbundle] Pathforresource:<span style="color: #800000;">@ "</span> <span style="color: #800000;">star Moon myth</span> <span style="color: #800000;">"</span> ofType:<span style="color: #800000;">@ "</span><span style="color: #800000;">mp3</span><span style="color: #800000;">"</span>] error:nil]; <span style="color: #008000;">//</span> <span style="color: #008000;"></span> *player = [[avaudioplayer alloc] initwithdata:data error:nil <span style="color: #000000;"></span> ]; <span style="color: #008000;">//</span> <span style="color: #008000;">Create with NSData</span></strong></span></pre></pre><span style="font-size: 15px;"><span style="font-size: 15px;">I've said before that Avaudio can not play the network url, but can play nsdata, we seem to be inspired, we can go through the network URL to create nsdata, and then through the Avaudioplayer to play nsdata, Is it possible to play online music? but this method is not advisable, because Avaudioplayer can only play a complete file, does not support streaming playback, so must be buffered to play, so if the network file is too large or is not enough speed is not to wait a long time? So play the network audio we generally use the audio Queue. </span></span><p><p><span style="font-size: 15px;">second, Player Properties</span></p></p><p><p><span style="font-size: 15px;">After you create a avaudioplayer, you can access or set up its various properties.</span></p></p><p><p><span style="font-size: 15px;">1. volume</span></p></p><p><p><span style="font-size: 15px;">Between Player.volume =0.8;//0.0-1.0</span></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">2. Number of cycles</span></span><p><p><span style="font-size: 15px;">Player.numberofloops =3;//play only once by default</span></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">3. Playback Position</span></span><p><p><span style="font-size: 15px;">Player.currenttime =15.0;//can Specify to start playback from any position</span></p></p><p><p><span style="font-size: 15px;">4. Number of channels</span></p></p><p><p><span style="font-size: 15px;">Nsuinteger Channels = Player.numberofchannels;//read-only Property</span></p></p><p><p><span style="font-size: 15px;">5. Duration</span></p></p><p><p><span style="font-size: 15px;">Nstimeinterval Duration = Player.duration;//get duration</span></p></p><p><p><span style="font-size: 15px;">6. Meter Counting</span></p></p><p><p><span style="font-size: 15px;">player.meteringenabled =yes;//turn on meter count function</span></p></p><p><p><span style="font-size: 15px;">[playerupdatemeters];//update Gauge Count</span></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">third, play the sound</span></span><p><p><span style="font-size: 15px;">[player Preparetoplay];//allocates the required resources for playback and joins them to the internal play queue</span></p></p><p><p><span style="font-size: 15px;">[playerplay];//playback</span></p></p><p><p><span style="font-size: 15px;">[playerstop];//stop</span></p></p><p><p><span style="font-size: 15px;">Iv. Agent Methods</span></p></p><p><p><span style="font-size: 15px;">This class corresponds to the avaudioplayerdelegater of the delegate Method. Audioplayerdidfinishplaying:successfully: triggered when audio playback is Complete. When playback is complete, you can reset the playback Button's text back to: play Audio File</span></p></p><pre><span style="font-size: 14px;"><strong>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) audioplayerdidfinishplaying: (avaudioplayer *<span style="color: #000000;"><span style="color: #000000;">) player Successfully: (BOOL) flag{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">actions performed at end of playback</span></span><span style="color: #000000;"><span style="color: #000000;">[audiobutton settitle:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Play Audio File</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">forstate:uicontrolstatenormal];}<br></span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Audioplayerdecodeerrordidoccur: (avaudioplayer *) player error: (nserror *<span style="color: #000000;"><span style="color: #000000;">) error; { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">decoding the actions performed by the error</span></span><span style="color: #000000;"><span style="color: #000000;">}<br></span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) audioplayerbegininterruption: (avaudioplayer *<span style="color: #000000;"><span style="color: #000000;">) player; { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">code to handle interrupts</span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #0000ff;">Audioplayerendinterruption: When the program is interrupted by an external application, it is triggered when it returns to the Application. When you return to this application here, continue playing the Music. </span></span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) audioplayerendinterruption: (avaudioplayer *<span style="color: #000000;"><span style="color: #000000;">) Player{[audioplayer play];}</span></span></strong></span></pre><p><p></p></p><p><p>Several ways to play audio on IOS</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service