启动和停止AVAudioPlayer时出现问题

时间:2010-07-10 12:55:11

标签: iphone objective-c avaudioplayer

我有一个课程,我有一个音频播放器。在B级,我有一个开关,从那里我想控制播放和停止背景音乐。

我无法通过开关停止播放音乐。 这是我在A类的viewdidload上的音频播放器

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/bgmusic.wav", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
if (audioPlayer == nil) NSLog([error description]);
else [audioPlayer play];

这是B级的开头

-(IBAction) musicValueChanged:(id)sender{    
if (musicSwitch.on) { 
    isStopped = FALSE;
    firstViewController.player.currentTime = 0;
    [firstViewController.audioPlayer play];
 }else {
    [firstViewController.audioPlayer volume:0];
 }
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

只要调用该方法并且musicSwitch.on有一个值,这应该有效:

- (IBAction)musicValueChanged:(id)发送者{

if (musicSwitch.on) { 
    isStopped = FALSE;
    firstViewController.player.currentTime = 0;
    [firstViewController.audioPlayer play];
 }else {
    [firstViewController.audioPlayer stop];
 }
}

请参阅AVAudioPlayer文档。