2012-04-27 28 views
0

我有一個應用程序,可以跨不同的ViewControllers播放和控制音樂。要做到這一點,我創建AVAudioPLayer的兩個實例中的應用程序委託的DidFinishLaunchingMethod:AVAudioPlayer在5.0中正常工作,在4.0-4.3.5中發佈問題

NSString *path = [[NSBundle mainBundle] pathForResource:@"minnie1" ofType:@"mp3"]; 
NSURL *path1 = [[NSURL alloc] initFileURLWithPath:path]; 
menuLoop =[[AVAudioPlayer alloc] initWithContentsOfURL:path1 error:NULL]; 
[menuLoop setDelegate:self]; 
menuLoop.numberOfLoops = -1; 
[menuLoop play]; 


NSString *path2 = [[NSBundle mainBundle] pathForResource:@"minnie2" ofType:@"mp3"]; 
NSURL *path3 = [[NSURL alloc] initFileURLWithPath:path2]; 
gameLoop=[[AVAudioPlayer alloc] initWithContentsOfURL:path3 error:NULL]; 
gameLoop.numberOfLoops = -1; 
[gameLoop setDelegate:self]; 
[gameLoop prepareToPlay]; 

這個我把它在各種viewControllers,停止或重新啓動使用類似的代碼後:

- (IBAction)playGameLoop{ 

NSLog(@"Begin playGameLoop"); 
FPAppDelegate *app = (FPAppDelegate *)[[UIApplication sharedApplication] delegate]; 

if ([FPAVManager audioEnabled] == NO){ 
    //DO NOTHING 
} 
else { 
    if (app.gameLoop.playing ==YES) { 
     //DO NOTHING 
    } 
    else { [app.gameLoop play]; 
    NSLog(@"End playGameLoop"); 
    } 
    } 

的音頻文件第一次玩的很好,當他們停下來時他們會停下來。雖然在iOS4設備上,當再次調用時,它們不會開始重放。

謝謝!

回答

0

從文檔:

 
Calling this method [stop] or allowing a sound to finish playing, 
undoes the setup performed upon calling the play or prepareToPlay 
methods. 

The stop method does not reset the value of the currentTime property 
to 0. In other words, if you call stop during playback and then call 
play, playback resumes at the point where it left off. 

如果你是循環的聲音,我會希望能夠調用stop,然後調用play。但我有模糊的回憶,自己遇到了這個問題。我發現撥打pause而不是stop是更好的解決方案。特別是因爲我可以再打電話play恢復比賽。在致電play之前,撥打stop總是需要再次撥打prepareToPlay,至少在我的經驗中。

iOS4.x和iOS5之間的API實現可能也有一些變化。您應該在開發人員網站上檢查API差異以確保。

+0

看來我已經解決了這個問題,至少在我測試過的一臺設備上。 雖然它可能不是正確的解決方案,但它似乎工作時,我從新的viewController內重新初始化播放器。 我還列出了menuLoop作爲此viewController中的一個屬性。現在看起來沒問題。謝謝大家。 – Fluffhead 2012-04-27 18:25:19

相關問題