audio学习相关

一. Play

AVAudioPlayer


Audio Queue: 适合stream…支持多路回放
For sample code, see the   SpeakHere   sample.

OpenAL: position
For sample code, see  oalTouch


二. Record

Determining Whether a Device Supports Recording
[[AVAudioSession sharedInstance] inputIsAvailable];


AVAudioRecorder

To prepare for recording using an audio recorder:

  1. Specify a sound file URL.

  2. Set up the audio session.

  3. Configure the audio recorder’s initial state. 

Audio Queue Services

your application instantiates a recording audio queue object and provides a callback function. The callback stores incoming audio data in memory for immediate use or writes it to a file for long-term storage.

三. Audio Unit Support in iOS

Audio unit

Description

iPod Equalizer unit

The iPod EQ unit, of type kAudioUnitSubType_AUiPodEQ, provides a simple, preset-based equalizer you can use in your application. For a demonstration of how to use this audio unit, see the sample code project iPhoneMixerEQGraphTest .

3D Mixer unit

The 3D Mixer unit, of type kAudioUnitSubType_AU3DMixerEmbedded, lets you mix multiple audio streams, specify stereo output panning, manipulate playback rate, and more. OpenAL is built on top of this audio unit and provides a higher-level API well suited for game apps.

Multichannel Mixer unit

The Multichannel Mixer unit, of type kAudioUnitSubType_- MultiChannelMixer, lets you mix multiple mono or stereo audio streams to a single stereo stream. It also supports left/right panning for each input. For a demonstration of how to use this audio unit, see the sample code project Audio Mixer (MixerHost) .

Remote I/O unit

The Remote I/O unit, of type kAudioUnitSubType_RemoteIO, connects to audio input and output hardware and supports realtime I/O. For demonstrations of how to use this audio unit, see the sample code project aurioTouch .

Voice Processing I/O unit

The Voice Processing I/O unit, of type kAudioUnitSubType_- VoiceProcessingIO, has the characteristics of the I/O unit and adds echo suppression and other features for two-way communication.

Generic Output unit

The Generic Output unit, of type kAudioUnitSubType_- GenericOutput, supports converting to and from linear PCM format; can be used to start and stop a graph.

   

Converter unit

The Converter unit, of type kAudioUnitSubType_AUConverter, lets you convert audio data from one format to another. You typically obtain the features of this audio unit by using the Remote I/O unit, which incorporates a Converter unit.

For more information on using system audio units, see  Audio Unit Hosting Guide for iOS

The iOS Dev Center provides two sample-code projects that demonstrate use of system audio units:  aurioTouch  and  iPhoneMultichannelMixerTest  .

Tips for Using Audio

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” (page 28) 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.



四. Audio Session
AddMusic  , a sample code project that demonstrates use of an audio session object in the context of a playback application. This sample also demonstrates coordination between application audio and iPod audio.



For application sound to play, or for recording to work, your audio session must be active

you can change the category as often as you need to, and can do so whether your session is active or inactive.

An audio session comes with some default behavior. Specifically:

  • Playback is enabled and recording is disabled.
  • When the user moves the Silent switch (or Ring/Silent switch on iPhone) to the “silent” position, your audio is silenced.
  • When the user presses the Sleep/Wake button to lock the screen, or when the Auto-Lock period expires, your audio is silenced.
  • When your audio starts, other audio on the device—such as iPod audio that was already playing—is silenced.


For playback that continues when the screen is locked, use the  AVAudioSessionCategoryPlayback

To ensure that iPod music is not interrupted, configure your audio session to allow mixing. Use the  AVAudioSessionCategoryAmbient

AVAudioSession 注意事项

  • A VoIP app, which spends most of its time running in the background, should ensure that its audio session is active only while the app is handling a call. In the background, standing ready to receive a call, a VoIP app’s audio session should not be active.
  • An app using the “recording” category should ensure that its audio session is active only while recording. Before recording starts and when recording stops, ensure your session is inactive to allow other sounds, such as incoming message alerts, to play. Alternatively, if your recording app also provides playback, you could switch to a playback category when not recording. This also allows alert sounds to play.

The system deactivates your audio session for a Clock or Calendar alarm or incoming phone call. :  Interruptions happen when a competing audio session from a built-in application activates and that session is not categorized by the system to mix with yours.

 


iOS has six audio session categories: (被系统影响或影响别的app)
  • Three for playback
  • One for recording
  • One that supports playback and recording—that need not occur simultaneously
  • One for offline audio processing
factory:  the Ring/Silent switch , the screen locks , other audio play or record

How interruptions work
  • AV Foundation framework : The system automatically pauses playback or recording upon interruption, and reactivates your audio session when you resume playback or recording.
  •  Audio Queue Services, I/O audio unit : You are responsible for saving playback or recording position and reactivating your audio session after interruption ends
  • OpenAL: implement the AVAudioSession interruption delegate methods or write an interruption listener callback function—as when using Audio Queue Services. However, the delegate or callback must additionally manage the OpenAL context. 

Route Changed 
One of the audio hardware route change reasons in iOS is kAudioSessionRouteChangeReason_CategoryChange. In other words, a change in audio session category is considered by the system—in this context—to be a route change, and will invoke a route change property listener callback.

factory:  plugs in or unplugs a headset, or docks or undocks the device, or  Bluetooth device connects or disconnects

To set hardware preferences, use the  AudioSessionSetProperty  function along with the  kAudioSessionProperty_PreferredHardwareSampleRate  kAudioSessionProperty_PreferredHardwareIOBufferDuration 
 Although you can safely specify hardware preferences at any time after the audio session is initialized, best practice is to do so while the session is inactive. After you establish hardware preferences, activate the audio session and then query it to obtain the actual values. This final step is important because the system may not be able to provide what you ask for.

 To obtain meaningful values for hardware characteristics, ensure that the audio session is initialized and active before you issue queries.

 
kAudioSessionProperty_AudioInputAvailable
Whether audio input is available on the device. Use this property to determine if audio recording is possible.


Working with Movie Players
In iOS3.1.3 and earlier , a movie player always uses a system-supplied audio session.To obtain that same behavior in iOS 3.2 and newer, you must set the movie player’s  useApplicationAudioSession  property value to  NO


If you have configured a movie player to use its own audio session, there’s some cleanup to perform. After a movie finishes, or the user dismisses it, do these two steps, in sequence, to regain the ability to play audio:

  1. Dispose of the movie player—even if you intend to play the same movie again later.

  2. Reactivate your audio session. 



Optimizing for Minimal Power Consumption
For an example of how to find, instantiate, and configure audio units, see the  iPhoneMultichannelMixerTest  sample.


#if TARGET_IPHONE_SIMULATOR
#warning *** Simulator mode: audio session code works only on a device
    // Execute subset of code that works in the Simulator
#else
    // Execute device-only code as well as the other code 
#endif

你可能感兴趣的:(audio学习相关)