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

转载于:https://my.oschina.net/dake/blog/196792

<think>嗯,用户问的是关于“audiopolicy 学习文档”,我需要先明确他们指的是哪个方面。Audiopolicy通常和Android音频策略有关,可能是Android系统中的音频管理模块。首先,我应该回忆一下Android的音频架构,特别是Audio Policy Service的作用。 用户可能是开发者,想要了解如何配置或调试音频策略,或者在学习Android音频系统的工作原理。需要确认audiopolicy是否指AudioPolicyManager相关的文档。如果是的话,我应该介绍AudioPolicyManager的基本功能,比如路由决策、设备管理、策略配置等。 接下来,结构要清晰,分点讲解。可能需要涵盖核心概念,比如音频设备类型、策略配置文件、路由规则等。然后,可以提到学习文档的位置,比如Android开源项目中的源码路径,或者在线资源。 还要考虑用户可能遇到的问题,比如如何修改策略,处理音频焦点,或者多设备切换。需要确保提供的步骤准确,并建议参考官方文档或源码。另外,注意使用中文,避免技术术语翻译错误,必要时保留英文术语并用中文解释。 最后,提醒用户查阅官方文档和社区资源,确保信息的可靠性。可能还需要检查是否有最新的Android版本变动,影响AudioPolicy部分的内容,确保回答不过时。</think>关于Android系统中的`audiopolicy`模块,以下是系统化的学习指南: 1. **基础概念** - **音频策略服务(Audio Policy Service)** 是Android音频系统的核心组件,负责管理音频流(如音乐、通话、通知)的优先级、设备路由(如扬声器/耳机切换)和音量控制。 $$ \text{输入:音频请求} \rightarrow \text{策略决策} \rightarrow \text{输出:设备/音量配置} $$ 2. **核心功能解析** - **路由策略** 根据设备状态(插入耳机/蓝牙连接)自动选择最优输出路径。例如: ```c // 示例:设备切换逻辑(伪代码) if (耳机已插入) { routeTo(HEADSET); } else { routeTo(SPEAKER); } ``` - **策略配置文件** 位于`/vendor/etc/audio_policy_configuration.xml`,定义设备能力与策略规则。 3. **关键代码结构** - **AudioPolicyManager** 核心类(代码路径:`frameworks/av/services/audiopolicy/managerdefault/`) - `getOutputForDevice()`:决定音频输出目标 - `setDeviceConnectionState()`:处理设备连接事件 4. **调试工具** - **adb命令** ```bash adb shell dumpsys media.audio_policy # 查看当前策略状态 adb shell audiopolicy --help # 策略调试工具 ``` - **日志过滤** ```bash adb logcat | grep "APM" # 查看AudioPolicyManager日志 ``` 5. **常见问题场景** - **多设备抢占** 例如通话时蓝牙耳机优先级高于扬声器,由`AUDIO_POLICY_FORCE_FOR_COMMUNICATION`标志控制 - **低延迟路径** 通过`AUDIO_OUTPUT_FLAG_FAST`标志启用快速音轨 6. **学习资源推荐** - 官方文档:Android Open Source Project (AOSP)中`Audio Policy`章节 - 源码分析:《深入理解Android音频系统》(机械工业出版社) - 调试案例:Android IssueTracker中`audiopolicy`标签问题 建议通过修改`audio_policy_configuration.xml`并观察`dumpsys`输出来验证策略变化,注意修改前需备份原始文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值