在下面的手機中,我正在爲我的應用中的四種不同聲音準備AVAudioPlayer。如果我不這樣做,它會導致應用程序延遲大約1.5秒,我只是在需要的地方進行操作。那麼是否有任何方法可以讓代碼看起來更好,或者更高效地工作,因爲現在我在App-Delegate中準備好了這些聲音,並且它稍微減慢了啓動速度。因此,這裏的代碼,讓我知道如果有什麼辦法讓下面更好的驗證碼更有效的代碼? (AVAudioPlayer)
- (void)readySounds {
NSString *path = [[NSBundle mainBundle] pathForResource:@"TrackUno" ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *one = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
self.tr1 = one;
[one release];
[tr1 prepareToPlay];
[tr1 setDelegate:self];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"TrackDos" ofType:@"mp3"];
NSURL *file2 = [[NSURL alloc] initFileURLWithPath:path2];
AVAudioPlayer *two = [[AVAudioPlayer alloc] initWithContentsOfURL:file2 error:nil];
[file2 release];
self.tr2 = two;
[two release];
[tr2 prepareToPlay];
[tr2 setDelegate:self];
NSString *path3 = [[NSBundle mainBundle] pathForResource:@"Click" ofType:@"mp3"];
NSURL *file3 = [[NSURL alloc] initFileURLWithPath:path3];
AVAudioPlayer *three = [[AVAudioPlayer alloc] initWithContentsOfURL:file3 error:nil];
[file3 release];
self.clk = three;
[three release];
[clk prepareToPlay];
[clk setDelegate:self];
NSString *path4 = [[NSBundle mainBundle] pathForResource:@"HSSound" ofType:@"mp3"];
NSURL *file4 = [[NSURL alloc] initFileURLWithPath:path4];
AVAudioPlayer *four = [[AVAudioPlayer alloc] initWithContentsOfURL:file4 error:nil];
[file4 release];
self.hs = four;
[four release];
[hs prepareToPlay];
[hs setDelegate:self];
}
我會如何重構它,使其看起來更好? –
@MyApps更新 – justin
Ohhh很好,順便說一句,這是這行:NSURL * url = [bundle URLForResource:fileName withExtension:extension]; 此外,這是在輔助線程,以便它將是最有效的,它可以是?編輯:恭喜獲得12K! –