开发者

Audio does not stop?

开发者 https://www.devze.com 2023-04-04 08:13 出处:网络
My small app plays music when the user hits the play button, but I want the music to stop when the user clicks on the \"next\" button. I\'ve tried different methods but I\'m having a problem; can anyo

My small app plays music when the user hits the play button, but I want the music to stop when the user clicks on the "next" button. I've tried different methods but I'm having a problem; can anyone help please? Please find attached the code. Thanks.

- (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] r开发者_StackOverflow社区esourcePath]]];
    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];
    }
}


You need to stop the AVAudioPlayer using the -stop method. For more information, see the AVAudioPlayer Class Reference. You would use this in the -play button action like this:

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


If I am reading this right, you want to stop the audioPlayer when the user hits next but it continues to play. I noticed that you don't have [audioPlayer release] or [audioPlayer stop] in (IBAction)next. Try that and see if it stops the player when next is selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消