1
我想在我的應用程序中的所有按鈕上添加自定義點擊聲音。我在它創建了一個類類別的UIButton用下面的代碼:與弧類類別AVAudioPlayer
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
if(audioPlayer == nil){
NSLog(@"%@", [error description]);
} else{
[audioPlayer play];
}
這一切都在執行:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
但是從我瞭解有一個與ARC常見的問題(至極我使用)在這裏。音頻播放器對象似乎在播放聲音之前釋放。我也看過AudioServicesPlaySystemSound();
。但目前我無法選擇這種方式,因爲我需要在應用程序中使用音量控制器,並且我沒有辦法在不離開應用程序的情況下更改系統音量。
我的問題是:有什麼辦法可以對UIButton類別中的audioPlayer對象進行強引用嗎?或者有沒有其他的,或許更好的方法來實現這個目標?
也許是關聯的對象? – 2013-03-18 09:09:41
這就像一個魅力!只是把它作爲我的問題的答案,我會接受它。 – 2013-03-18 09:35:34