AVAudioEngine: no sound

1

I'm trying to test AVAudioEngine, watched the WWDC conferences about it, and did everything by the book to try to play a simple file.

Despite everything being exactly the same as in the samples that I found (WWDC and a few other places), despite everything seeming fine (no error, seems to be running), I have no sound output.

Here's the code:

NSError *error = Nil;

[AVAudioSession.sharedInstance setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error: &error];
[AVAudioSession.sharedInstance setActive:YES error:&error];

AVAudioEngine *engine = [[AVAudioEngine alloc] init];

AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"piano" withExtension:@"wav"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:fileURL error:&error];

AVAudioMixerNode *mainMixer = [engine mainMixerNode];
AVAudioOutputNode *outputNode = engine.outputNode;

[engine connect:player to:mainMixer format:file.processingFormat];
[engine connect:engine.mainMixerNode to:outputNode format:nil];
[engine prepare];

if (!engine.isRunning) {
    if (![engine startAndReturnError:&error]) {
        //TODO
    }
}

[player scheduleFile:file atTime:nil completionHandler:nil];
[player play];

NSLog(@"Engine description: %@", [engine description]);

Here's the output of the log call:

Engine description: 
________ GraphDescription ________
AVAudioEngineGraph 0x10100d820: initialized = 1, running = 1, number of nodes = 3

     ******** output chain ********

     node 0x28388d200 {'auou' 'rioc' 'appl'}, 'I'
         inputs = 1
             (bus0, en1) <- (bus0) 0x2838a0880, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     node 0x2838a0880 {'aumx' 'mcmx' 'appl'}, 'I'
         inputs = 1
             (bus0, en1) <- (bus0) 0x282a8df00, {'augn' 'sspl' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         outputs = 1
             (bus0, en1) -> (bus0) 0x28388d200, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     node 0x282a8df00 {'augn' 'sspl' 'appl'}, 'I'
         outputs = 1
             (bus0, en1) -> (bus0) 0x2838a0880, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
______________________________________

I have no idea how to diagnose this and can't find any mistake or reason for which this would not work.

Any idea?

ios
objective-c
avaudioengine
asked on Stack Overflow Jan 18, 2021 by Stéphane

1 Answer

1

Keeping the question in case anyone has this problem: as I was testing, all my variables were local variables in a test method, and as soon as the method was finished it was probably GC by ARC or something.

Having the AVAudioEngine variable outside (keeping a reference to it) fixed it.

answered on Stack Overflow Jan 18, 2021 by Stéphane • edited Jan 18, 2021 by Stéphane

User contributions licensed under CC BY-SA 3.0