Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to stop sound / audio

kevintarr131kevintarr131 Posts: 28New Users @
edited January 2013 in iOS SDK Development
Hi all,
I have two buttons that play two different sounds in my app. I want to create a stop button that will stop playing all the sounds if pressed. Is anybody able to provide me with help on how to go about making a stop button? My code for playing the sounds are below. Any help / guidance is appreciated. Thanks


]]-(IBAction)sound1 {

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @test1, CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);

}

-(IBAction)sound2 {

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @test2, CFSTR("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);

}

Replies

  • mashercakesmashercakes Posts: 789Registered Users @ @ @
    You could use an AVAudioPlayer. That way you can retain the player in a property and then call its stop method.
    <a href="http://itunes.apple.com/app/talkboard/id416436888?mt=8"; target="_blank">TalkBoard</a> - a communication aid and visual support app for children and adults with autism, communication difficulties or learning difficulties. Available now for iPad, iPhone and iPod Touch.
  • kevintarr131kevintarr131 Posts: 28New Users @
    Thanks, I will have a look into the AVAudioPlayer. I'm assuming my code as above would need to change and instead I would use the play method? Sorry for the stupid question, I am still new and teaching myself. Thanks
  • mashercakesmashercakes Posts: 789Registered Users @ @ @
    No worries. Yes you'll need to change all your playing code. Here's a sample from my app TalkBoard. In this example, [self prPlayers] is an NSMutableArray which holds all of the players. This is so that I can loop through the array and stop all of the players if I want to stop all sound.
    
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:&error];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    audioPlayer.numberOfLoops = 0;
    audioPlayer.delegate = self;
    [[self prPlayers] addObject:audioPlayer];
    [audioPlayer release];
    [audioPlayer play];
    
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
        [[self prPlayers] removeObject:player];
    }
    
    - (void)stopAllSounds {
        for (AVAudioPlayer *player in [self prPlayers]) {
            [player stop];
            [self audioPlayerDidFinishPlaying:player successfully:YES];
        }
    }
    
    If you want to stop a particular player, you could do:
    
    AVAudioPlayer *player = [[self players] objectAtIndex:x];
    [player stop];
    
    <a href="http://itunes.apple.com/app/talkboard/id416436888?mt=8"; target="_blank">TalkBoard</a> - a communication aid and visual support app for children and adults with autism, communication difficulties or learning difficulties. Available now for iPad, iPhone and iPod Touch.
  • kevintarr131kevintarr131 Posts: 28New Users @
    Thanks, was a real help. I've managed to amend mine and have it working now. Really appreciate your help!
  • mashercakesmashercakes Posts: 789Registered Users @ @ @
    Glad it helped :)
    <a href="http://itunes.apple.com/app/talkboard/id416436888?mt=8"; target="_blank">TalkBoard</a> - a communication aid and visual support app for children and adults with autism, communication difficulties or learning difficulties. Available now for iPad, iPhone and iPod Touch.
Sign In or Register to comment.