簡體   English   中英

當我的iPhone應用程序運行時,如何在后台播放連續音樂?

[英]How can I play continuous music in the background when my iPhone app is running?

當用戶積極運行我的應用程序時,我想運行輕音樂(不間斷)。 當用戶退出我的應用程序時,音樂將自動停止。 我嘗試了以下代碼來執行連續音樂播放(即循環播放音樂)。 但是它實際上並沒有循環。 一旦說完音樂,音樂就會結束。

- (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];
}

有人可以建議一些方法來實現循環播放連續音樂嗎?

我還沒有機會進行測試,但是您是否嘗試將numberOfLoops屬性設置為負值? AVAudioPlayer API文檔指示這是使聲音無限循環的正確方法。

組,

player.numberOfLoops = -1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM