這個問題可能是過於空泛通過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