完成播放后,简单的音频引擎isBackgroundMusicPlaying仍为YES

时间:2014-05-29 12:29:49

标签: iphone cocos2d-iphone

我需要检查背景音乐是​​否每秒播放一次,如果它没有播放,那么我会随机开始播放另一首歌曲。

此方法每秒调用

- (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],但我得到了同样的结果。

1 个答案:

答案 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];