iOS offers a rich set of tools for working with sound in your application. These tools are arranged into
frameworks according to the features they provide, as follows:
-
Use the Media Player framework to play songs, audio books, or audio podcasts from a user’s iPod library. For details, seeMedia Player Framework Reference, iPod Library Access Programming Guide, and the AddMusic sample code project.
-
Use the AV Foundation framework to play and record audio using a simple Objective-C interface. For details, see AV Foundation Framework Reference and the avTouch sample code project.
-
Use the Audio Toolbox framework to play audio with synchronization capabilities, access packets of incoming audio, parse audio streams, convert audio formats, and record audio with access to individual packets. For details, see Audio Toolbox Framework Reference and the SpeakHere sample code project.
-
Use the Audio Unit framework to connect to and use audio processing plug-ins. For details, see Audio Unit Hosting Guide for iOS.
-
Use the OpenAL framework to provide positional audio playback in games and other applications. iOS supports OpenAL 1.1. For information on OpenAL, see the OpenAL website, OpenAL FAQ for iPhone OS, and the oalTouch sample code project.
1.Use the Media Player framework --------------- ------ipod music
3.Use the AV Foundation framework ------- ------ 一般声音播放,录音,常用
4.Using Audio Queue Services --------(c语言)功能比AV Foundation 更强大
6.Use the Audio Unit framework
7.Use the OpenAL framework
.audio session 混音模式
Audio Session Programming Guide
,
About iPod Library Access
your application has two ways to retrieve items. The media picker, shown on the left, is an easy-to-use, pre-packaged view controller that behaves like the built-in iPod application’s music selection interface. For many applications, this is sufficient.
If the picker doesn’t provide the specialized access control that you want, the media query interface—shown to the lower right of your application in the figure—will. It supports predicate-based specification of items from the device iPod library.
Figure 1-1 Using iPod library access
You can obtain two flavors of music player, depending on the goals of your application.
-
The application music player plays music locally within your application. It is not influenced by, nor does it affect, the state of the built-in iPod application. Specifically, your music player has a different now-playing item, playback state, and modes. When a user quits your application, music that you are playing stops.
-
[MPMusicPlayerController applicationMusicPlayer]
-
The iPod music player, in effect, employs the built-in iPod app on your behalf; it shares the iPod’s state. When a user quits an app that is using this player, the music continues to play.
-
[MPMusicPlayerController iPodMusicPlayer]
Finally, keep in mind the following two important points about using music players:
Schematic representation of a music player object
MPMediaPickerController 系统界面选择歌曲
MPMusicPlayerController 用法和MPMoviePlayerController 类似。 一个是音频,一个视频,音频都可以。
MPMusicPlayerController 只能播放 系统中选择出来的歌曲。
MPMusicPlayer
controller is only for playing items from the user's iPod library. The AVAudioPlayer
will play encoded files from your app's sandbox, and you should be able to convert this code to useAVAudioPlayer
easily enough... looks like it would be:
NSString *url = [[NSBundle mainBundle] pathForResource:@"song1" ofType:@"mp3"];
AVAudioPlayer *myMusicPlayer=[[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:url]
error:nil];
[myMusicPlayer play];
MPMediaItemCollection 所有歌曲的集合描述类
MPMediaItem 单个歌曲的描述类
[selfsetMusicPlayer:[MPMusicPlayerControlleriPodMusicPlayer]];
// [self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]];
//
// // of the built-in iPod app. Here they are both turned off.
// [musicPlayer setShuffleMode: MPMusicShuffleModeOff];
// [musicPlayer setRepeatMode: MPMusicRepeatModeNone];
[musicPlayer setQueueWithItemCollection:mediaItemCollection];
[ musicPlayer play];
Using a media query to access the device iPod library
MPMediaQuery *everything = [[MPMediaQueryalloc]init];
NSArray *itemsFromGenericQuery = [everythingitems];
for (MPMediaItem *songin itemsFromGenericQuery) {
NSString *songTitle = [songvalueForProperty:MPMediaItemPropertyTitle];
//例子 2
MPMediaPropertyPredicate *artistNamePredicate = |
[MPMediaPropertyPredicate predicateWithValue: @"Happy the Clown" |
forProperty: MPMediaItemPropertyArtist]; |
|
MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init]; |
[myArtistQuery addFilterPredicate: artistNamePredicate]; |
|
NSArray *itemsFromArtistQuery = [myArtistQuery items]; |
Playing UI Sound Effects or Invoking Vibration Using System Sound Services
The AudioServicesPlaySystemSound
function lets you very simply play short sound files. The simplicity carries with it a few restrictions. Your sound files must be:
-
No longer than 30 seconds in duration
-
In linear PCM or IMA4 (IMA/ADPCM) format
-
Packaged in a .caf
, .aif
, or .wav
file
In addition, when you use the AudioServicesPlaySystemSound
function:
-
Sounds play at the current system audio volume, with no programmatic volume control available
-
Sounds play immediately
-
Looping and stereo positioning are unavailable
-
Simultaneous playback is unavailable: You can play only one sound at a time
The similar
AudioServicesPlayAlertSound
function plays a short sound as an alert. If a user has configured their device to vibrate in Ring Settings, calling this function invokes vibration in addition to playing the sound file.
Creating a sound ID object
// Get the main bundle for the app |
CFBundleRef mainBundle = CFBundleGetMainBundle (); |
|
// Get the URL to the sound file to play. The file in this case |
// is "tap.aif" |
soundFileURLRef = CFBundleCopyResourceURL ( |
mainBundle, |
CFSTR ("tap"), |
CFSTR ("aif"), |
NULL |
); |
|
// Create a system sound object representing the sound file |
AudioServicesCreateSystemSoundID ( |
soundFileURLRef, |
&soundFileObject |
); |
- (IBAction) playSystemSound { |
AudioServicesPlaySystemSound (self.soundFileObject); |
} |
Triggering vibration
#import <AudioToolbox/AudioToolbox.h> |
#import <UIKit/UIKit.h> |
- (void) vibratePhone { |
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); |
} |
Playing Sounds with Control Using Audio Queue Services
Audio Queue Services adds playback capabilities beyond those available with the AVAudioPlayer
class. Using Audio Queue Services for playback lets you:
-
Precisely schedule when a sound plays, allowing synchronization
-
Precisely control volume on a buffer-by-buffer basis
-
Play audio that you have captured from a stream using Audio File Stream Services
Audio Queue Services lets you play sound in any audio format available in iOS, as described in Table 1-1. You can also use this technology for recording, as explained in “Recording Audio.”
For detailed information on using this technology, see Audio Queue Services Programming Guide and Audio Queue Services Reference. For sample code, see the SpeakHere sample.
Recording
Recording with the AVAudioRecorder Class
To prepare for recording using an audio recorder:
-
Specify a sound file URL.
-
Set up the audio session.
-
Configure the audio recorder’s initial state.
Recording with Audio Queue Services
Parsing Streamed Audio
To play streamed audio content, such as from a network connection, use Audio File Stream Services in concert with Audio Queue Services.Audio File Stream Services parses audio packets and metadata from common audio file container formats in a network bitstream. You can also use it to parse packets and metadata from on-disk files.
iPhone OS系统上的音频单元支持
iPhone OS提供一组音频插件,称为音频单元,可以用于所有的应用程序。您可以通过Audio Unit框架提供的接口来打开、连接、和使用音频单元;还可以定义定制的音频单元,在自己的应用程序内部使用。由于应用程序必须静态连接定制的音频单元,所以iPhone OS系统上的其它应用程序不能使用您开发的音频单元。
表7-3列出了iPhone OS提供的音频单元。
表7-3 系统提供的音频单元
音频单元 | 描述 |
---|
转换器单元 | 转换器单元,类型为kAudioUnitSubType_AUConverter ,用于音频数据的格式转换。 |
iPod均衡器单元 | iPod EQ单元,类型为kAudioUnitSubType_AUiPodEQ ,提供一个简单的、基于预设的均衡器,可以在应用程序中使用。 |
3D混音器单元 | 3D混音器单元,类型为kAudioUnitSubType_AU3DMixerEmbedded ,用于混合多个音频流,指定立体声输出移动,操作采样率,等等。 |
多通道混音器单元 | 多通道混音器单元,类型为kAudioUnitSubType_MultiChannelMixer ,用于将多个音频流混合成为单一的音频流。 |
一般输出单元 | 一般输出单元,类型为kAudioUnitSubType_GenericOutput ,支持和线性PCM格式互相转换,可以用于开始或结束一个音频单元图。 |
I/O单元 | I/O单元,类型为kAudioUnitSubType_RemoteIO ,用于连接音频输入和输入硬件,支持实时I/O。如何使用音频单元的实例代码请见aurioTouch工程。 |
语音处理I/O单元 | 语音处理I/O单元,类型为kAudioUnitSubType_VoiceProcessingIO ,具有I/O单元的特征,同时为了支持双向交流,加入了回响抑制功能。 |
有关系统音频单元的更多信息,请参见系统音频单元访问指南。
iPhone音频的最佳实践
操作音频的贴士
在操作iPhone OS系统上的音频内容时,您需要记住表7-4列出的基本贴士。
表7-4 音频贴士
贴士 | 动作 |
---|
正确地使用压缩音频 | 对于AAC、MP3、和ALAC (Apple Lossless) 音频,解码过程是由硬件来完成的,虽然比较有效,但同时只能解码一个音频流。如果您需要同时播放多路声音,请使用IMA4 (压缩) 或者线性PCM (无压缩) 格式来存储那些文件。 |
将音频转换为您需要的数据格式和文件格式 | Mac OS X的afconvert 工具可以进行很多数据格式和文件类型的转换。请参见“iPhone OS偏好的音频格式” 部分和afconvert 工具的手册页面。 |
评价音频的内存使用问题 | 当您使用音频队列服务播放音频时,需要编写一个回调函数,负责将较短的音频数据片断发送到音频队列的缓冲区。在某些情况下,将整个音频文件载入内存是最佳 的选择,这样可以使播放时的磁盘访问尽最少;而在另外一些情况下,最好的方法则是每次只载入足够填满缓冲区的数据。请测试和评价哪种策略对您的应用程序最 好。 |
限制音频的采样率和位深度,减少音频文件的尺寸 | 采样率和每个样本的位深度对无压缩音频的尺寸有直接的影响。如果您需要播放很多这样的声音,则应该考虑降低这些指标,以减少音频数据的内存开销。举例来 说,相对于使用采样率为44.1 kHz的音频作为声音效果, 您可以使用采样率为32 kHz(或可能更低)的音频,仍然可以得到很合理的品质。 |
选择恰当的技术 | 使用Core Audio的系统声音服务来播放警告和用户界面声音效果。当您希望使用便利的高级接口来定位立体声场中的声音,或者要求很低的回放延迟时,则应该使用 OpenAL。如果需要从文件或网络数据流中解析出音频数据,可以使用音频文件服务接口。如果只是简单回放一路或多路声音,则应该使用AVAudioPlayer 类。对于具有其它音频功能的应用程序,包括音频流的回放和音频录制,可以使用音频队列服务。 |
低延迟编码 | 如果需要尽可能低的回放延迟,可以使用OpenAL,或者直接使用I/O单元。 |
iPhone OS偏好的音频格式
对于无压缩(最高品质)音频,请使用封装在CAF文件中的、16位、低位在前(little endian)的线性PCM音频数据。您可以用Mac OS X的afconvert
命令行工具来将音频文件转换为上述格式:
/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}
|
afconvert
工具可以进行广泛的音频数据格式和文件类型转换。您可以通过afconvert
的手册页面,以及在shell提示符下键入afconvert -h
命令获取更多信息。
对于压缩音频,当每次只需播放一个声音,或者当不需要和iPod同时播放音频时,适合使用AAC格式的CAF或m4a文件。
当您需要在同时播放多路声音时减少内存开销时,请使用IMA4 (IMA/ADPCM) 压缩格式,这样可以减少文件尺寸,同时在解压缩过程中对CPU的影响又最小。和线性PCM数据一样,请将IMA4数据封装在CAF文件中。
Tips for Using Audio
Table 1-6 lists some important tips to keep in mind when using audio in iOS.
Table 1-6 Audio tips
Tip | Action |
---|
Use compressed audio appropriately | For AAC, MP3, and ALAC (Apple Lossless) audio, decoding can take place using hardware-assisted codecs. While efficient, this is limited to one audio stream at a time. If you need to play multiple sounds simultaneously, store those sounds using the IMA4 (compressed) or linear PCM (uncompressed) format. |
Convert to the data format and file format you need | The afconvert tool in Mac OS X lets you convert to a wide range of audio data formats and file types. See “Preferred Audio Formats in iOS” and the afconvert man page. |
Evaluate audio memory issues | When playing sound with Audio Queue Services, you write a callback that sends short segments of audio data to audio queue buffers. In some cases, loading an entire sound file to memory for playback, which minimizes disk access, is best. In other cases, loading just enough data at a time to keep the buffers full is best. Test and evaluate which strategy works best for your application. |
Reduce audio file sizes by limiting sample rates, bit depths, and channels | Sample rate and the number of bits per sample have a direct impact on the size of your audio files. If you need to play many such sounds, or long-duration sounds, consider reducing these values to reduce the memory footprint of the audio data. For example, rather than use 44.1 kHz sampling rate for sound effects, you could use a 32 kHz (or possibly lower) sample rate and still provide reasonable quality. Using monophonic (single-channel) audio instead of stereo (two channel) reduces file size. For each sound asset, consider whether mono could suit your needs. |
Pick the appropriate technology | Use OpenAL when you want a convenient, high-level interface for positioning sounds in a stereo field or when you need low latency playback. To parse audio packets from a file or a network stream, use Audio File Stream Services. For simple playback of single or multiple sounds, use the AVAudioPlayer class. For recording to a file, use the AVAudioRecorder class. For audio chat, use the Voice Processing I/O unit. To play audio resources synced from a user’s iTunes library, use iPod Library Access. When your sole audio need is to play alerts and user-interface sound effects, use Core Audio’s System Sound Services. For other audio applications, including playback of streamed audio, precise synchronization, and access to packets of incoming audio, use Audio Queue Services. |
Code for low latency | For the lowest possible playback latency, use OpenAL or use the I/O unit directly. |
Preferred Audio Formats in iOS
For uncompressed (highest quality) audio, use 16-bit, little endian, linear PCM audio data packaged in a CAF file. You can convert an audio file to this format in Mac OS X using the afconvert
command-line tool, as shown here:
/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT} |
The afconvert
tool lets you convert to a wide range of audio data formats and file types. See the afconvert
man page, and enter afconvert -h
at a shell prompt, for more information.
For compressed audio when playing one sound at a time, and when you don’t need to play audio simultaneously with the iPod application, use the AAC format packaged in a CAF or m4a file.
For less memory usage when you need to play multiple sounds simultaneously, use IMA4 (IMA/ADPCM) compression. This reduces file size but entails minimal CPU impact during decompression. As with linear PCM data, package IMA4 data in a CAF file.