当您启动您的应用程序时,您可以像这样检查并播放或不播放您自己的音乐。
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
// dont play and do whatever you think should be done in this case
} else {
[[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];
}
边注:
//Remember to preload your effects and bg music so there is not any delay
[[SimpleAudioEngine sharedEngine] preloadEffect:@"aaa.caf"];
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"song1.mp3"];
//And to unload your effects when you wont use them anymore or receive a mem warning, or in dealloc
[[SimpleAudioEngine sharedEngine] unloadEffect:@"aaa.caf"];
更新
在 viewDidLoad 中,创建一个包含所有歌曲的字符串数组,并使用:[self shuffle] 对其进行随机播放,为此实现以下代码:
- (void)shuffle {
for (NSInteger i = [songsArray count] - 1; i > 0; --i) [songsArray exchangeObjectAtIndex:(arc4random() % (i+1)) withObjectAtIndex:i];
}
每次您的背景音乐完成使用时[self shuffle]
,playBackgroundMusic:[songsArray lastObject]
都应该处理您的随机播放列表。