帮助,AVAudioEngine附加并连接AVAudioUnitSampler可以正常播放。但是在调用AVAudioEngine停止并重新启动后,播放时崩溃。我的代码如下:
self.engine=[[AVAudioEngine alloc] init];
self.sampler= [AVAudioUnitSampler new];
NSError *error=nil;
NSURL soundfontFileURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"sf2"]];
[_sampler loadSoundBankInstrumentAtURL:soundfontFileURL program:0 bankMSB:0x79 bankLSB:0 error:&error];
[self.engine attachNode:_sampler];
[self.engine connect:_sampler to:[self.engine outputNode] format:nil];
if([self.engine startAndReturnError:&error]){
NSLog(@"start ok ");
}else{
NSLog(@"start err: %@",error);
}
//play at here is ok.
//[_sampler startNote:60 withVelocity:100 onChannel:0];
[self.engine stop];
if([self.engine startAndReturnError:&error]){
NSLog(@"start ok ");
}else{
NSLog(@"start err: %@",error);
}
//after stop and start again,play crash AudioStreamerImpl::sIOWorkerProcess (17)
[_sampler startNote:60 withVelocity:100 onChannel:0];
崩溃信息: libEmbeddedSystemAUs.dylib`DLSSample :: GetMoreFrames: AudioStreamerImpl :: sIOWorkerProcess(17):EXC_BAD_ACCESS(代码= 1,地址= 0x701c943f1a0)
我几个月来在我的应用中遇到了这样的问题。经过一些测试,在启动引擎和弹奏音符之间重新加载SoundFont时,似乎没有出现死机。
engine.stop()
do {
try engine.start()
reloadSoundFont() // remove this, and there will be the crash with GetMoreFrames()
} catch {
print("Could not start engine: " + error.localizedDescription)
}
self.sampler.startNote(60, withVelocity: 100, onChannel: 0)
delay(1.0) {
self.sampler.stopNote(60, onChannel: 0)
}