这个问题可能是过于空泛通过StackOverflow's
标准,但我必须尝试张贴,因为我跑出来的选项...:/AVAudioPlayer和随机慢度
长话短说:我有一个应用程序,经历随机时间的缓慢。它不会经常发生(可能每个月都会发生一次),但是当它执行时,只需完整重启即可运行iDevice。症状是:2-3秒的响应时间和动荡不安,动荡不安;整个应用程序基本上变得无法使用。
我已经通过每种可能的诊断工具运行该应用程序,但没有发现任何错误;没有内存泄漏或CPU使用率异常高。但是,这并不令人惊讶,因为该应用程序非常简单,适用于纸牌游戏的跟踪器应用程序。
所有这些让我相信,当用户点击button
时,我用来播放声音可能是问题的原因(它是整个应用程序中唯一的,相对较高的复杂元素)。然而,我不确定,哪里是我需要帮助的地方。我在这里包含一个示例代码,也许有人在iOS
音频播放经验可以看看它,看看是否有我忽略了一些错误。
这里,我们去: 首先,我初始化“沉默的玩家认为”剩打一个无声的轨道每秒保持AVAudioPlayer
活着。这是必要的,因为在较长时间不活动之后被呼叫的相对较长的响应时间AVAudioPlayer
。
NSString *silenceFilePath = [[NSBundle mainBundle] pathForResource: @"silence" ofType: @"wav"];
NSURL *silenceFileURL = [[NSURL alloc] initFileURLWithPath: silenceFilePath];
silencePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: silenceFileURL error: nil];
[silencePlayer setDelegate: self];
[silencePlayer prepareToPlay];
silenceTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(repeatSilence)
userInfo:nil
repeats:YES];
的调用的NSTimer以下方法:
- (void) repeatSilence
{
if (isAudioON == YES)
{
if (silencePlayer.playing)
{
[silencePlayer stop];
silencePlayer.currentTime = 0;
[silencePlayer play];
}
else
{
[silencePlayer play];
}
}
}
其余是相当简单的。我启动另一个AVAudioPlayer发挥特定的按钮声音(有两个的这些):
NSString *buttonSoundFilePath = [[NSBundle mainBundle] pathForResource: @"button_pushed" ofType: @"wav"];
NSURL *buttonFileURL = [[NSURL alloc] initFileURLWithPath: buttonSoundFilePath];
buttonPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: buttonFileURL error: nil];
[buttonPlayer setDelegate: self];
[buttonPlayer prepareToPlay];
而当按钮被按下我播放声音(相同的方式,我用沉默的玩家所做的那样):
if (isAudioON == YES)
{
if (buttonPlayer.playing)
{
[buttonPlayer stop];
buttonPlayer.currentTime = 0;
[buttonPlayer play];
}
else
{
[buttonPlayer play];
}
}
真的只有它。不过,我担心,尽管这种方法很简单,但这种连续的音频播放功能创造了一个罕见的iOS实例。但所有这些只是一个理论,这就是为什么我需要有更多经验的人来看看我的代码并分享他的观点。
谢谢!
更新: 我发现了另外几行也可能与问题相关的代码。有了它们,我设置了AVAudioPlayer与音乐播放的背景(从另一个应用程序例如)同时工作:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setDelegate:self];
好的见解。我会在这个周末给他们一个尝试并分享结果! – 2013-05-09 19:10:52