2009-11-24 187 views
1

當用戶正在主動運行我的應用程序時,我想運行輕音樂(不停止)。當用戶退出我的應用程序時,音樂將自動停止。我嘗試了下面的代碼來執行連續的音樂播放(即,在循環中播放音樂)。但它並不實際循環;一旦音樂完成,它結束音樂。當我的iPhone應用程序正在運行時,如何在後臺播放連續音樂?

- (void) PlayLoopMusic { 
    NSString* path; 
    NSURL* url; 
    path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
    url = [NSURL fileURLWithPath:path]; 

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; 
    [player prepareToPlay]; 
    [player setVolume: 0.030]; 
    [player setDelegate: self]; 

    // Start playing it while starting the game ... 
    [player play]; 
} 

有人可以建議實現循環,連續音樂的方法嗎?

+4

請有一種方法來關閉音樂。大多數人會因爲播放音樂而無法關閉的應用程序(尤其是網頁)而非常惱火。 – rmeador 2009-11-24 22:08:40

+0

Ofcourse,我確實...感謝您的建議。 – Getsy 2009-11-25 05:39:42

回答

3

我還沒有機會測試這個,但你有沒有嘗試將numberOfLoops屬性設置爲負值? AVAudioPlayer API docs表明這是讓聲音無限循環的正確方法。

+0

非常好。 numberOfLoops添加爲-1,並且它連續運行。 – Getsy 2009-11-25 05:33:45

2

集,

player.numberOfLoops = -1;

相關問題