gpt4 book ai didi

iOS Objective-C 低延迟音频播放,如 SoundPool

转载 作者:行者123 更新时间:2023-11-29 02:09:15 24 4
gpt4 key购买 nike

嘿,在我的 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 音频缓冲区随时可以播放。

关于iOS Objective-C 低延迟音频播放,如 SoundPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com