IOS audio playback
Sound support is essential in the development of iPhone applications or games. Each of the several applications I have done involves sound effects, so here is a simple induction, many of them refer to the iPhone application programming guide (which requires an Apple ID to open the link) and add some practical experience.
IPhone OS mainly provides the following methods for playing audio:
System sound services
Avaudioplayer class
Audio queue services
Openal
1. system sound services
system sound services is the most basic and simple sound playback service. You can call audioservicesplaysystemsound to play some simple audio files, this method is only suitable for playing small prompts or warning sounds, because it has many restrictions:
■ The sound length must be less than 30 seconds
■ in linear PCM or ima4 (IMA/ADPCM) format
■ package. CAF ,. AIF, or. W AV files
■ playback progress cannot be controlled
■ play a sound immediately after calling the method
■ No loop playback or stereo Control
In addition, it can also call the system vibration function, and the method is also very simple. For more information about Code , see syssound
, however, the official examples only have some simple usage. From the documentation, we can add the callback function for audio playback through the audioservicesaddsystemsoundcompletion method. With the callback function, we can solve many problems, for example, system sound services does not support loop playback. The following code implements a background music loop in the Program :
?
12345678910111213141516171819202122232425262728293031 |
Static
Void Completioncallback (systemsoundid myssid ){
// Play again after Sound Play completion
Audioservicesplaysystemsound (myssid ); |