简体   繁体   English

AVAudioEngine:没有声音

[英]AVAudioEngine: no sound

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.我正在尝试测试 AVAudioEngine,观看了有关它的 WWDC 会议,并按照书本上的所有内容尝试播放一个简单的文件。

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.尽管一切都与我发现的样本(WWDC 和其他一些地方)完全相同,尽管一切看起来都很好(没有错误,似乎正在运行),但我没有声音 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:这是日志调用的 output:

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?任何想法?

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.保留这个问题以防万一有人遇到这个问题:当我测试时,我所有的变量都是测试方法中的局部变量,一旦方法完成,它可能是 ARC 的 GC 或其他东西。

Having the AVAudioEngine variable outside (keeping a reference to it) fixed it.将 AVAudioEngine 变量放在外面(保持对它的引用)修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM