此时我有一个简单的要求,一个iOS应用程序,它从音频文件读取并使用AudioUnits输出到扬声器。不使用高级API的原因是,在某些时候,我需要处理来自音频文件的样本,并最终通过网络发送它。
我有一个有效的代码,读取音频文件并播放到扬声器。这里唯一的问题是,渲染回调不起作用。回调永远不会被调用,注册时也不会收到任何错误。非常感谢帮助(我是Core Audio的初学者,这是我关于stackoverflow的第一个问题,所以请原谅任何基本错误/忽视)。我附上了用于初始化图表的代码段。
void createMyAUGraph (MyAUGraphPlayerST *player) {
// Create a new AUGraph
CheckError(NewAUGraph(&player->graph), "New AUGraph failed");
// Generate description for output
AudioComponentDescription outputcd = {0};
outputcd.componentType = kAudioUnitType_Output;
outputcd.componentSubType = kAudioUnitSubType_RemoteIO;
outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
outputcd.componentFlags = 0;
outputcd.componentFlagsMask = 0;
// Add new node
AUNode outputNode;
CheckError(AUGraphAddNode(player->graph, &outputcd, &outputNode), "Add output node failed");
// Node for file player
AudioComponentDescription fileplayercd = {0};
fileplayercd.componentType = kAudioUnitType_Generator;
fileplayercd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
fileplayercd.componentManufacturer = kAudioUnitManufacturer_Apple;
// Add new node
AUNode fileNode;
CheckError(AUGraphAddNode(player->graph, &fileplayercd, &fileNode), "Add file node failed");
// Open graph
CheckError(AUGraphOpen(player->graph), "Graph open failed");
// Retrive AudioUnit
CheckError(AUGraphNodeInfo(player->graph, outputNode, NULL, &player->outputAU), "file unit retrive failed");
CheckError(AUGraphNodeInfo(player->graph, fileNode, NULL, &player->fileAU), "file unit retrive failed");
// connect nodes
CheckError(AUGraphConnectNodeInput(player->graph, fileNode, 0, outputNode, 0), "failed to connect nodes");
// some other setup
UInt32 flag = 1;
CheckError(AudioUnitSetProperty(player->outputAU,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0,
&flag,
sizeof (flag)), "Set io property failed");
// Register render callback
AURenderCallbackStruct output_cb;
output_cb.inputProc = recording_cb;
output_cb.inputProcRefCon = player;
CheckError(AudioUnitSetProperty(player->outputAU, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &output_cb, sizeof (output_cb)), "callback register failed");
// initialize graph
CheckError(AUGraphInitialize(player->graph), "graph initialization failed");
}