我需要检查背景音乐是否每秒播放一次,如果它没有播放,那么我会随机开始播放另一首歌曲。
此方法每秒调用
- (void)private_checkMusicEnds {
if (NUM_BGMUSIC > 0) {
if ([ModuleHelper audio_isBackgroundMusicEnabled]) {
//ERROR: BACKGROUND MUSIC NOT PLAYING BUT ALWAYS IS PLAYING???
if ([SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying == NO) {
if ([CacheHelper isFirstTimeSetDefaultBGMusic]) {
_currentPlayingMusic = DEFAULT_BGMUSIC_INDEX;
}
else {
//song start at 1
_currentPlayingMusic = [Helper getRandomIntBothInclusiveLow:1 high:NUM_BGMUSIC];
}
NSString *fn = [NSString stringWithFormat:FORMAT_STRING_SONG_FILE_NAME, _currentPlayingMusic];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:fn loop:NO];
}
}
}
}
歌曲播放完毕后,[SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying
仍为YES。
我也试过了[[CDAudioManager sharedManager].backgroundMusic isPlaying]
,但我得到了同样的结果。
答案 0 :(得分:1)
我最近的cocos2d游戏遇到了类似的问题。
您需要在播放音乐之前设置CDAudioManager模式(自最新的ios更新后可能需要它)
//required to play bgmusic
[[CDAudioManager sharedManager] setMode:kAMM_MediaPlayback];
NSString *fn = [NSString stringWithFormat:FORMAT_STRING_SONG_FILE_NAME, _currentPlayingMusic];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:fn loop:NO];