Ios Can';我似乎没有停止背景音乐

Ios Can';我似乎没有停止背景音乐,ios,objective-c,avaudioplayer,Ios,Objective C,Avaudioplayer,在我的viewcontroller.h文件中,我有: - (void)startBackgroundMusic; - (void)stopBackgroundMusic; @property (strong, nonatomic) AVAudioPlayer *audioPlayer; 在我的viewcontroller.m中,我有: - (void)startBackgroundMusic { NSError *err; NSURL *file = [NSURL file

在我的viewcontroller.h文件中,我有:

- (void)startBackgroundMusic;

- (void)stopBackgroundMusic;

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
在我的viewcontroller.m中,我有:

- (void)startBackgroundMusic
{
    NSError *err;
    NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@" DK" ofType:@"mp3"]];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
    if (err) {
        NSLog(@"error in audio play %@",[err userInfo]);
        return;
    }
    [_audioPlayer prepareToPlay];

    _audioPlayer.numberOfLoops = -1;
    [_audioPlayer setVolume:.5];
    [_audioPlayer play];
}

- (void) stopBackgroundMusic {  
    [_audioPlayer pause];
}
在MyScene.m文件中,当我单击已关闭的开关时,我运行:

[vc stopBackgroundMusic];
我还有:

ViewController *vc;
在上面声明并导入viewcontroller.h


这不管用。它弹得很好,但我就是停不下来。这似乎很容易完成,但可能我遗漏了什么。

尝试停止而不是暂停。startBackgroundMusic可能应该在分配新的AVAudioPlayer之前调用stop。也许可以检查startBackgroundMusic中的audioPlayer是否为零。也许你有两个同时播放?好吧,我把暂停切换到停止,我用if(_audioPlayer==nil){包围了startBackgroundMusic中的代码,但它仍然不起作用。当我的场景加载并且它不起作用时,我试图调用停止,所以好像它没有与ViewController通信,所以发生了一些事情