Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

iphone - Cocoa audio issues

  1. I have 2 buttons, 1 turns to the next page and should play a sound at the same time but the sound doesn't seems to be working, I need your help on this please.

  2. I have 4 pages and in each page there is a play button that play or stop the audio, but if I press play and press next to the next page, the audio will keep playing, how do I do so that if I press the next button, it will stop the music automatically. Thanks for your help, find below the code.

    - (IBAction)next {
    
        // This part plays the next page turn noise
        NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.mp3", [[NSBundle mainBundle] resourcePath]]];
    
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
        [audioPlayer setDelegate:self];
        audioPlayer.numberOfLoops = 0;
        [audioPlayer play];
    
        // 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 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];
        }
    }
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your method which handles the page turn, you need to stop the current playing sound by using [audioPlayer stop] if it is playing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...