从 Springboard 返回后导致通用跳过的 AudioUnit
我在使用 AudioUnit 的应用程序中遇到问题。所有应用程序的音频(包括不是通过AudioUnit播放的音频)在退出Springboard并返回到应用程序后都会开始跳跃。
我将问题分解为一个新的单独的测试应用程序。以下是重复该操作的步骤:
- 使用 AVAudioPlayer。
- 创建、删除,然后再次创建 AudioUnit
- 退出到 Springboard
- 返回应用程序
- 来自 AvAudioPlayer 的音频将开始跳过
以下是我使用的一些代码:
- (IBAction)restartAudioUnit {
MySoundStream* audioUnitClass;
audioUnitClass = Load();
[audioUnitClass release];
audioUnitClass = Load();
}
请原谅长代码转储,但 AudioUnit 很复杂,我相当确定我只是错误地设置或删除了它们。
MySoundStream 类:
OSStatus UnitRenderCB(void* pRefCon, AudioUnitRenderActionFlags* flags, const AudioTimeStamp* timeStamp, UInt32 busNum, UInt32 numFrames, AudioBufferList* pData){
OSStatus tErr = noErr;
//Do Nothing
return tErr;
}
@implementation MySoundStream
-(void) dealloc {
[self Unload];
[super dealloc];
}
-(void) Unload {
OSStatus tErr = noErr;
tErr = AudioUnitUninitialize(OutUnit);
}
@end
MySoundStream* Load()
{
OSStatus tErr = noErr;
AudioComponentInstance tRIO;
AudioComponentDescription tRIOCD;
AURenderCallbackStruct tRIOCB;
AudioStreamBasicDescription tAUF;
tRIOCD.componentType = kAudioUnitType_Output;
tRIOCD.componentSubType = kAudioUnitSubType_RemoteIO;
tRIOCD.componentManufacturer = kAudioUnitManufacturer_Apple;
tRIOCD.componentFlags = 0;
tRIOCD.componentFlagsMask = 0;
AudioComponent tRIOC = AudioComponentFindNext(NULL, &tRIOCD);
tErr = AudioComponentInstanceNew(tRIOC, &tRIO);
if (tErr != noErr) return NULL;
int tOutEnable = 1;
tErr = AudioUnitSetProperty(tRIO, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &tOutEnable, sizeof(tOutEnable));
if (tErr != noErr) return NULL;
tAUF.mSampleRate = 44100.00;
tAUF.mFormatID = kAudioFormatLinearPCM;
tAUF.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
tAUF.mFramesPerPacket = 1;
tAUF.mChannelsPerFrame = 2;
tAUF.mBitsPerChannel = 16;
tAUF.mBytesPerPacket = 4;
tAUF.mBytesPerFrame = 4;
tErr = AudioUnitSetProperty(tRIO, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &tAUF, sizeof(tAUF));
if (tErr != noErr) return false;
MySoundStream* pRet = [MySoundStream alloc];
tRIOCB.inputProc = UnitRenderCB;
tRIOCB.inputProcRefCon = pRet;
tErr = AudioUnitSetProperty(tRIO, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &tRIOCB, sizeof(tRIOCB));
if (tErr != noErr){ delete pRet; return NULL; }
tErr = AudioUnitInitialize(tRIO);
if (tErr != noErr){ delete pRet; return NULL; }
pRet->OutUnit = tRIO;
return pRet;
}
如果有人能看到我在这个 AudioUnit 上做错了什么,那将会非常有帮助。
编辑: 上传完整源码。 此处
- 按播放声音(可能需要耳机)
- 按 RestartAudioUnit
- 返回 Springboard
- Re -输入 TestAudioUnit 应用程序
音频将跳过
I have a problem in my applications where I am using AudioUnits. All of the applications Audio (including audio not played through AudioUnits) will start skipping after exiting to Springboard and returning to the applications.
I broke out the problem into a new separate test app. Here are the steps to repeat it:
- Start an Audio file playing using an
AVAudioPlayer. - Create, Delete, then again Create an
AudioUnit - Exit to Springboard
- Return to the app
- The Audio from the AvAudioPlayer will start skipping
Here is some of the code I used:
- (IBAction)restartAudioUnit {
MySoundStream* audioUnitClass;
audioUnitClass = Load();
[audioUnitClass release];
audioUnitClass = Load();
}
Forgive the long code dump but AudioUnits are complex and I am fairly sure I am just setting them up or taking them down incorrectly.
The MySoundStream class:
OSStatus UnitRenderCB(void* pRefCon, AudioUnitRenderActionFlags* flags, const AudioTimeStamp* timeStamp, UInt32 busNum, UInt32 numFrames, AudioBufferList* pData){
OSStatus tErr = noErr;
//Do Nothing
return tErr;
}
@implementation MySoundStream
-(void) dealloc {
[self Unload];
[super dealloc];
}
-(void) Unload {
OSStatus tErr = noErr;
tErr = AudioUnitUninitialize(OutUnit);
}
@end
MySoundStream* Load()
{
OSStatus tErr = noErr;
AudioComponentInstance tRIO;
AudioComponentDescription tRIOCD;
AURenderCallbackStruct tRIOCB;
AudioStreamBasicDescription tAUF;
tRIOCD.componentType = kAudioUnitType_Output;
tRIOCD.componentSubType = kAudioUnitSubType_RemoteIO;
tRIOCD.componentManufacturer = kAudioUnitManufacturer_Apple;
tRIOCD.componentFlags = 0;
tRIOCD.componentFlagsMask = 0;
AudioComponent tRIOC = AudioComponentFindNext(NULL, &tRIOCD);
tErr = AudioComponentInstanceNew(tRIOC, &tRIO);
if (tErr != noErr) return NULL;
int tOutEnable = 1;
tErr = AudioUnitSetProperty(tRIO, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &tOutEnable, sizeof(tOutEnable));
if (tErr != noErr) return NULL;
tAUF.mSampleRate = 44100.00;
tAUF.mFormatID = kAudioFormatLinearPCM;
tAUF.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
tAUF.mFramesPerPacket = 1;
tAUF.mChannelsPerFrame = 2;
tAUF.mBitsPerChannel = 16;
tAUF.mBytesPerPacket = 4;
tAUF.mBytesPerFrame = 4;
tErr = AudioUnitSetProperty(tRIO, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &tAUF, sizeof(tAUF));
if (tErr != noErr) return false;
MySoundStream* pRet = [MySoundStream alloc];
tRIOCB.inputProc = UnitRenderCB;
tRIOCB.inputProcRefCon = pRet;
tErr = AudioUnitSetProperty(tRIO, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &tRIOCB, sizeof(tRIOCB));
if (tErr != noErr){ delete pRet; return NULL; }
tErr = AudioUnitInitialize(tRIO);
if (tErr != noErr){ delete pRet; return NULL; }
pRet->OutUnit = tRIO;
return pRet;
}
If anyone can see anything I am doing wrong with this AudioUnit, that woudl be very helpful.
Edit:
Upload the complete source. Here
- Press Play Sound (may need headphones)
- Press RestartAudioUnit
- Return to Springboard
- Re-enter TestAudioUnit app
Audio will skip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当应用程序重新初始化时,您正在调用
AudioUnitInitialize()
,这不好。您只需在应用启动时调用AudioUnitInitialize()
一次,并且不必在应用每次进入前台时都构建整个 AU 图表。You are calling
AudioUnitInitialize()
when the app is re-initialized, which is not good. You need to callAudioUnitInitialize()
only once when your app starts, and you should not have to build the entire AU graph every time your app enters the foreground.改用 AUGraph 来设置我的音频单元路径,我的运气更好了。
Switched to using an AUGraph to setups my Audio Unit path and I had better luck.