开发者

Stopping the audio after playing with AVAudioPlayer

开发者 https://www.devze.com 2023-02-12 10:51 出处:网络
Hi guys hope you all can help me with this issue. What I would like to do is when user left view B to view A, the audio plays, when user left view A to view B, the audio stops.

Hi guys hope you all can help me with this issue.

What I would like to do is when user left view B to view A, the audio plays, when user left view A to view B, the audio stops.

so when user left view B (removing viewB from superview) back to the main view which is view A, the methods playIT & hahaPlay will be called. When user left view A to View B (view A add subview B) the stopPlaying method will be called instead. But here's my problem, even the stopPlaying method is called, the audio is not stopped. I realised the alert is shown but for some reason the statement> [audioPlayer stop]; is not functioning.

I've already defined the audio player in header file just fyi.

All the methods below are in the viewcontroller called soundView.

Sorry I might be doing the whole thing in a wrong way, maybe some of you can enligh开发者_开发百科ten me and correct my mistakes?

-(void)playIT{

    filePath = [[NSBundle mainBundle] pathForResource:@"6431" 
                                               ofType:@"aiff"];
    fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    audioPlayer = [[AVAudioPlayer alloc] 
                   initWithContentsOfURL:fileURL error:nil];
}

-(void)hahaPlay{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"playing" message:@" playing" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil];
    [alert show]; 
    [alert release]; 
    [audioPlayer prepareToPlay];
    if(![audioPlayer isPlaying]){

        [audioPlayer play];
    }

- (void)stopPlaying{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stop playing" message:@"Stop playing" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil];
    [alert show]; 
    [alert release]; 

    [audioPlayer stop];

}


Maybe you're losing the reference somewhere by mistake, try setting a breakpoint near the stop call, and checking audioPlayer just in case that the audioPlayer is nil. Just saying because usually this is the reason for most of these type of problems for me...

Also will the following work?

[audioPlayer pause]
audioPlayer.currentTime = 0;
0

精彩评论

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