Hier ist eine komplette, wenn auch etwas rau, Funktion, die einige Audio zu Testzwecken spielen wird (eine andere die Datei auswählen, wenn Sie Garage dort nicht installiert haben, natürlich). Um zu vermeiden, dass eine Geräte-ID hart codiert wird, wechselt sie zu Ihrem Alarmgerät ("Soundeffekte"), das Sie in den Systemeinstellungen einstellen können.
AVAudioEngine *engine = [[AVAudioEngine alloc] init];
AudioUnit outputUnit = engine.outputNode.audioUnit;
OSStatus err = noErr;
AudioDeviceID outputDeviceID;
UInt32 propertySize;
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDefaultSystemOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
propertySize = sizeof(outputDeviceID);
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, &outputDeviceID);
if (err) { NSLog(@"AudioHardwareGetProperty: %d", (int)err); return; }
err = AudioUnitSetProperty(outputUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &outputDeviceID, sizeof(outputDeviceID));
if (err) { NSLog(@"AudioUnitSetProperty: %d", (int)err); return; }
NSURL *url = [NSURL URLWithString:@"/Applications/GarageBand.app/Contents/Frameworks/MAAlchemy.framework/Versions/A/Resources/Libraries/WaveNoise/Liquid.wav"];
NSError *error = nil;
AVAudioFile *file = [[AVAudioFile alloc] initForReading:url error:&error];
if (file == nil) { NSLog(@"AVAudioFile error: %@", error); return; }
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];
[engine connect:player to:engine.outputNode format:nil];
NSLog(@"engine: %@", engine);
if (![engine startAndReturnError:&error]) {
NSLog(@"engine failed to start: %@", error);
return;
}
[player scheduleFile:file atTime:[AVAudioTime timeWithHostTime:mach_absolute_time()] completionHandler:^{
NSLog(@"complete");
}];
[player play];
Als Totalen, habe ich versucht, die 'mainMixerNode' eines' AVAudioEngine' zu einem 'AVAudioIONode' Gießen, die tatsächlich funktioniert (aus irgendeinem Grund?) - es ist ein Anfang. – benjineer