音频不停?

时间:2011-09-11 22:58:11

标签: iphone objective-c xcode

我的小应用程序在用户点击播放按钮时播放音乐,但我希望当用户点击“下一步”按钮时音乐停止。我尝试了不同的方法,但我遇到了问题;有人可以帮忙吗?请查找随附的代码。感谢。

- (IBAction)info {

    // This part takes us to the info page
    InfoPage *rab = [[InfoPage alloc] initWithNibName:@"InfoPage" bundle:nil];
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [self.view addSubview:rab.view];
    [UIView commitAnimations];

    // This part plays the next page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    pagePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [pagePlayer setDelegate:self];
    pagePlayer.numberOfLoops = 0;
    [pagePlayer play];

    //This will stop the music when turning page
    //[audioPlayer release];
    clicked = 0;
    [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];

}

- (IBAction)next {

    // This part takes us to the next view
    Rabbana2 *rab = [[Rabbana2 alloc] initWithNibName:@"Rabbana2" bundle:nil];
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [self.view addSubview:rab.view];
    [UIView commitAnimations];

    // This part plays the next page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    pagePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [pagePlayer setDelegate:self];
    pagePlayer.numberOfLoops = 0;
    [pagePlayer play];

    //This will stop the music when turning page
    //[audioPlayer release];
    clicked = 0;
    [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal]; 

}

// This button plays the audio
- (IBAction)play {

    if(clicked == 0){
        clicked = 1;
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/rabbana1.wav", [[NSBundle mainBundle] resourcePath]]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        [audioPlayer setDelegate:self];
        audioPlayer.numberOfLoops = 0;


        [audioPlayer play];
        [start setImage:[UIImage imageNamed:@"Sstop.png"] forState:UIControlStateNormal];

    } 
    else{
        [audioPlayer release];
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    } 

}

//If user does not do anything by the end of the sound set the button to start
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
                        successfully: (BOOL) flag {
    if (flag==YES) {
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    }
}

// If user click on the next button while audio playing, Audio should stop
- (void) audioPlayerDidNotFinishPlaying: (AVAudioPlayer *) player
                                success: (BOOL) flag {
    if (flag==YES) {
        //[audioPlayer stop];
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    }
}

2 个答案:

答案 0 :(得分:0)

您需要使用AVAudioPlayer方法停止-stop。有关更多信息,请参阅AVAudioPlayer Class Reference。您可以在-play按钮操作中使用此功能,如下所示:

- (IBAction)play {
    if(clicked == 0){
    ...
    } else {
        [audioPlayer stop];
        [audioPlayer release];
        ...
    }
}

答案 1 :(得分:0)

如果我正在读这个,你想在用户点击下一步时停止audioPlayer,但它会继续播放。我注意到你的(IBAction)接下来没有[audioPlayer release]或[audioPlayer stop]。尝试一下,看看是否在选择下一个时停止播放器。