iOS objective C 像 SoundPool 一样的低延迟音频播放

iOS objective C low-latency audio playback like SoundPool

嘿,在我的 Android 应用程序上,我可以在 SoundPool 中预加载我的声音,然后几乎没有延迟地播放它们。现在我在 iOS/obj-c 上寻找同样的东西,但我找不到类似的东西。

我遵循了几个教程,但最终出现了比我预期的更大的滞后,大多数教程都建议您将音频转换为未压缩格式,如 wav 或 caf,但我的 MP3 已经是 14 mb 并且将它们转换为无损音频会产生 81 MB 的数据,这对我来说太多了。

我尝试过的最有前途的事情是预加载文件(就像我在 Android 的 SoundPool 中所做的那样),如这个 OAL 示例所示:

- (bool) preloadUrl:(NSURL*) url seekTime:(NSTimeInterval)seekTime
{
    if(nil == url)
    {
        OAL_LOG_ERROR(@"%@: Cannot open NULL file / url", self);
        return NO;
    }

    OPTIONALLY_SYNCHRONIZED(self)
    {
        // Bug: No longer re-using AVAudioPlayer because of bugs when using multiple players.
        // Playing two tracks, then stopping one and starting it again will cause prepareToPlay to fail.

        bool wasPlaying = playing;

        [self stopActions];

        if(playing || paused)
        {
            [player stop];
        }

        as_release(player);

        if(wasPlaying)
        {
            [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:[NSNotification notificationWithName:OALAudioTrackStoppedPlayingNotification object:self] waitUntilDone:NO];
        }

        NSError* error;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if(nil == player)
        {
            OAL_LOG_ERROR(@"%@: Could not load URL %@: %@", self, url, [error localizedDescription]);
            return NO;
        }

        player.volume = muted ? 0 : gain;
        player.numberOfLoops = numberOfLoops;
        player.meteringEnabled = meteringEnabled;
        player.delegate = self;
        player.pan = pan;

        as_release(currentlyLoadedUrl);
        currentlyLoadedUrl = as_retain(url);

        self.currentTime = seekTime;
        playing = NO;
        paused = NO;

        BOOL allOK = [player prepareToPlay];
        if(!allOK)
        {
            OAL_LOG_ERROR(@"%@: Failed to prepareToPlay: %@", self, url);
        }
        else
        {
            [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:[NSNotification notificationWithName:OALAudioTrackSourceChangedNotification object:self] waitUntilDone:NO];
        }
        preloaded = allOK;
        return allOK;
    }
}

但这仍然会产生大约 60 毫秒的相当大的延迟,这对于像我这样的音频应用程序来说太多了。我的音频文件在开始时没有任何延迟,所以它必须对代码进行一些处理。

我在 iPhone 5c 上尝试了所有这些东西。

应该 能够创建多个 AVAudioPlayer 并对其调用 prepareToPlay,但我个人喜欢使用 AVAssetReader保留一个 LPCM 音频缓冲区,随时可以播放。