2011-10-15 53 views
1

在下面的手機中,我正在爲我的應用中的四種不同聲音準備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]; 
    } 

回答

2

如果初始化時間是你唯一關心的,那麼你可能*在輔助線程創建這個和完善感知發射時間。然後它會啓動流和解碼器,並從輔助線程從磁盤打開音頻文件。

如果回放,流計數,內存使用情況,CPU,加載時間,解碼,過濾,重新採樣等仍然是因素:您正在使用最高級別的API,可以下拉幾個級別並擁有所有控制你想要了解這些方面的每一個方面。

通過重構它可以使它看起來更好 - 該方法執行四次相似的序列。


* 「可能」,因爲我只使用了低級別的API,我不知道是否有AVAudioPlayer線程的限制。


實例添加了錯誤檢查重構:

沒有編制,但你的想法:

- (AVAudioPlayer *)newAudioPlayerForFile:(NSString *)fileName extension:(NSString *)extension inBundle:(NSBundle *)bundle 
{ 
    assert(fileName && extension && bundle); 
    NSURL * url = [bundle URLForResource:fileName ofType:extension]; 
    if (nil == url) { 
    assert(0 && "could not locate resource"); 
    return nil; 
    } 

    NSError * error = 0; 
    AVAudioPlayer * spieler = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
    assert(spieler); 

    if (nil != error) { 
    NSLog(@"Error encountered creating audio player for file (%@): %@", fileName, error); 
    [spieler release], spieler = nil; 
    return nil; 
    } 

    [spieler prepareToPlay]; 
    [spieler setDelegate:self]; 

    return spieler; 
} 

- (void)readySounds 
{ 
    NSAutoreleasePool * pool = [NSAutoreleasePool new]; 
    NSBundle * mainBundle = [NSBundle mainBundle]; 
    self.tr1 = [[self newAudioPlayerForFile:@"TrackUno" extension:@"mp3" inBundle:mainBundle] autorelease]; 
    self.tr2 = [[self newAudioPlayerForFile:@"TrackDos" extension:@"mp3" inBundle:mainBundle] autorelease]; 
    self.clk = [[self newAudioPlayerForFile:@"Click" extension:@"mp3" inBundle:mainBundle] autorelease]; 
    self.hs = [[self newAudioPlayerForFile:@"HSSound" extension:@"mp3" inBundle:mainBundle] autorelease]; 
    [pool release], pool = nil; 
} 
+0

我會如何重構它,使其看起來更好? –

+0

@MyApps更新 – justin

+2

Ohhh很好,順便說一句,這是這行:NSURL * url = [bundle URLForResource:fileName withExtension:extension]; 此外,這是在輔助線程,以便它將是最有效的,它可以是?編輯:恭喜獲得12K! –

1

我發現,解決在後臺加載是一個問題...它的工作,然後第30次,我使用主線程的聲音,所有的聲音停止工作。有幾件事情可能發生 - 我必須創建自己的autorelease池,並且後臺線程可能已經不再使用該對象。

關於它的思考我只有很短的聲音效果 - 創建第一個AVAudioPlayer的速度很慢,但後來的效果還不錯。所以爲了解決這個問題,我創建了一個小型的無聲wav文件,並將其用作第一個加載的文件。我用大膽來創建文件,並做了曲目>添加新>音軌,然後我做了生成>沉默,選擇了一個非常小的間隔,如.01s,然後我File> Export'ed聲音並將文件添加到XCode。

我強制將小型無聲wav文件加載到viewDidLoad中,這爲我縮短了延遲。

希望這對有類似問題的人有幫助。