iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker

http://eureka.ykyuen.info/2010/06/02/iphone-configuring-the-avaudiosession-and-output-audio-to-iphone-speaker/


iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker

I am working on the AVAudioRecorder and AVAudioPlayer. By default, the output audio is routed to the receiver instead of the iPhone speaker. In order to fulfill my requirement, i have to configure the AVAudioSession in the AppDelegate.

There are 2 ways to route the audio output to speaker.

  • Overriding the output audio route
  • Changing the default output audio route (iPhone OS 3.1 or above)


If you only overriding the output audio route, the audio output will revert to the receiver instead of the speaker when u plugging in and then unplugging the headset. Therefore, it’d be better to change the default output audio route. but please note that it only support iPhone OS 3.1 or above.

Here comes to the codes.
1. Load the AVFoundation and AVToolbox frameworks into your Xcode project.

2. Import the header file in the AppDelegate.h

1 #import <AVFoundation/AVFoundation.h>
2 #import <AudioToolbox/AudioToolbox.h>

 

3. Add the following code in applicationDidFinishLaunching method in AppDelegate.m

01 // Set AudioSession
02 NSError *sessionError = nil;
03 [[AVAudioSession sharedInstance] setDelegate:self];
04 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
05  
06 /* Pick any one of them */
07 // 1. Overriding the output audio route
08 //UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
09 //AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
10  
11 // 2. Changing the default output audio route
12 UInt32 doChangeDefaultRoute = 1;
13 AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

 

Then u can try the playback with the AVAudioPlayer
 

Done =)

Reference: Audio Session Cookbook – Redirecting Output Audio


<think> 嗯,用户这次问的是Simulink报错的具体解决方案。从错误信息看,系统建议在输出端口后插入Signal Conversion模块,这通常涉及信号类型或存储类的配置问题。 用户提供的引用材料很有价值。引用[1]直接提到了插入Signal Conversion块的解决方案,并解释了存储类配置的调整位置。引用[2]则补充了Simulink信号分类的背景知识,特别是虚拟/非虚拟总线等概念,这有助于理解错误的深层原因。 考虑到用户是工程师,可能需要立即解决模型编译问题。应该重点给出可操作步骤: 1. 首先明确错误根源是输出端口信号属性不匹配,可能是存储类设置冲突或总线信号处理不当 2. 解决方案的核心操作就是添加Signal Conversion模块,这个模块在Simulink Library的Signal Attributes分类下 3. 需要特别说明存储类的转移操作,这是引用[1]强调的关键点 4. 对于总线信号问题,引用[2]提到的虚拟/非虚拟总线区别很重要,要指导用户如何通过Signal Conversion统一信号类型 用户可能还会遇到后续问题,比如: - 如何验证修改后的信号属性 - 其他相关错误代码的排查 - 自动修复脚本的使用 所以补充这些问题作为延伸思考很必要。 最后要注意术语的准确性,比如"存储类"不能写成"存储类型","非虚拟总线"要区分virtual bus的译法。示例代码框用m语言标注更专业,因为Simulink模型常用m脚本做批量处理。 </think> 根据Simulink报错信息和引用内容,解决方案如下(关键步骤已加粗): ### 错误原因分析 此报错通常由以下原因引发[^1][^2]: 1. **存储类(Storage Class)冲突**:输出端口设置了非`Auto`的存储类(如`ExportedGlobal`),但信号类型不支持 2. **信号类型不匹配**:总线(Bus)信号/向量信号未正确转换 3. **硬件部署限制**:FPGA/HDL代码生成时信号接口不兼容 --- ### 解决方案(分步操作) #### 方法1:插入信号转换模块(推荐) 1. **定位报错端口** 双击报错信息定位到模型中的输出端口(Outport) 2. **添加Signal Conversion模块** ```m [原始输出端口] → [Signal Conversion块] → [新建输出端口] ``` - 模块路径:`Simulink > Signal Attributes > Signal Conversion` - 配置参数:`Output > Convert to` 选择`Nonvirtual Bus`(总线信号)或`Element`(标量/向量) 3. **转移存储类设置** 将原输出端口的存储类设置**移动到Signal Conversion模块的输出端**: ```diff - 原输出端口存储类:ExportedGlobal → 改为 Auto + 新Signal Conversion输出端:存储类设为 ExportedGlobal ``` #### 方法2:分离输出通道(适用总线信号) ```m [原始总线信号] → [Bus Selector] → ├─[信号分支1] → [输出端口A](设置存储类) └─[信号分支2] → [输出端口B](设置存储类) ``` --- ### 配置示例 ```matlab % 验证信号类型的命令行检查(调试用) get_param('model_name/Outport','CompiledBusType') % 查看端口总线类型 sigHand = get_param('model_name/SignalConv','PortHandles'); set_param(sigHand.Outport,'StorageClass','ExportedGlobal'); % 设置存储类 ``` --- ### 注意事项 1. **总线信号处理** - 虚拟总线(Virtual Bus)可通过Signal Conversion转为非虚拟总线(Nonvirtual Bus)[^2] - 使用`Bus Creator`生成的信号需明确数据类型 2. **代码生成场景** FPGA/HDL部署时需确保: ```matlab Signal Conversion模块的"Output as nonvirtual bus"=on ``` 3. **信号维度检查** 通过`Display > Signals & Ports > Signal Dimensions`显示信号维度,避免多维信号未转换 > 附:Simulink信号分类参考[^2] > ```mermaid > graph LR > A[Simulink信号] --> B[标量/向量/矩阵] > A --> C[虚拟总线] > A --> D[非虚拟总线] > A --> E[函数调用] > ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值