1、描述音频单元

AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;

2、查找音频单元

AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

3、获取音频单元实例

status = AudioComponentInstanceNew(inputComponent, &audioUnit);

4、启用录制功能、启用播放功能

UInt32 flag = 1;
status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
kInputBus,
&flag,
sizeof(flag)); status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
kOutputBus,
&flag,
sizeof(flag));

5、音频流描述

AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;

6、应用录制和播放的音频流描述

status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputBus,
&audioFormat,
sizeof(audioFormat)); status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputBus,
&audioFormat,
sizeof(audioFormat));

7、设置回调

AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = recordingCallback;
callbackStruct.inputProcRefCon = self;
status = AudioUnitSetProperty(audioUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
kInputBus,
&callbackStruct,
sizeof(callbackStruct)); callbackStruct.inputProc = playbackCallback;
callbackStruct.inputProcRefCon = self;
status = AudioUnitSetProperty(audioUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
kOutputBus,
&callbackStruct,
sizeof(callbackStruct));

8、回调方法

static OSStatus recordingCallback(void *inRefCon,
AudioUnitRenderActionFlags*ioActionFlags,
const AudioTimeStamp*inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) { AudioBufferList *bufferList; OSStatus status = AudioUnitRender([(shockmanViewController*)inRefCon audioUnit], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, bufferList); return noErr; } static OSStatus playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags*ioActionFlags,
const AudioTimeStamp*inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
// This is a mono tone generator so we only need the first buffer
UInt16 *buffer = (SAMPLE *)ioData->mBuffers[0].mData;
// Generate the samples
for (UInt32 frame = 0; frame < inNumberFrames; frame++)
{
if(THIS->isPlaying) {
    // 通过修改buffer数组的值,输出自己的音频数据
buffer[frame] = audioProtocol.getEncode()[THIS->qIndex++];
if(THIS->qIndex >= THIS->qSize) {
//[THIS stopToPlay];
[THIS performSelectorOnMainThread:@selector(stopToPlay) withObject:nil waitUntilDone:NO];
break;
}
}
} return noErr; }

  

最新文章

  1. linux source
  2. Node进阶:核心模块http简介
  3. iOS UITableView中点击状态栏无法回滚到顶部
  4. LMT 装机记录
  5. django ORM中update_or_create功能,如果只要匹配某一特定字段呢
  6. 怎样在多线程中使用JNI?
  7. SSD 和 SAS 意外造 raid 1
  8. javascript计算啤酒2元一瓶,4个盖换一瓶,2个瓶换一瓶,10元钱最多喝多少瓶
  9. ●ZOJ 2112 Dynamic Rankings
  10. java 文件目录树
  11. js判断当前浏览器页面是否切换
  12. IDEA项目找不到浏览器报错的情况
  13. container injection——容器技术
  14. rabbitmq初学之连接测试
  15. iomanip的作用 C++
  16. idea Connection to SQL Server - 公网8 failed java
  17. Rancher与OpenLDAP对接
  18. 转:Ubuntu 10.10 安装后上不了网的原因
  19. CentOS7查看开放端口命令及开放端口号
  20. 〖Linux〗noip免费域名申请,及更新域名的API

热门文章

  1. Js Map 实现
  2. 准循环LDPC码用于公钥密码时的奇偶校验矩阵
  3. 20个人艰不拆的事实:知道真相的我眼泪掉下来 T.T
  4. JavaScript实现输入验证(简单的用户注册)
  5. mysql执行大量sql语句
  6. git 提交代码到github错误处理
  7. NGUI制作属于自己Button
  8. java通过文件路径读取该路径下的所有文件并将其放入list中
  9. PHP 数据安全问题总结
  10. Windows Store App 全球化 资源匹配规则