Chris
Chris

Reputation: 200

AVAudioPlayer Stop Sounds iPhone SDK Objective C

hope you can help me (tried lots of things here, but nothing worked)

i want to play several sounds by pressing a button and i want one button to cancel the playing sounds.

header:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface MainView : UIView <AVAudioPlayerDelegate>  {

    IBOutlet UILabel *SoundDescriptionLabel;
    IBOutlet UIBarButtonItem *StopSoundButton;

}
- (IBAction)PlayFatGuyWalking:(id)sender;
- (IBAction)PlayRoadRunner:(id)sender;
- (IBAction)ShowInfoPopup:(id)sender;
- (IBAction)PlayChaosRunning:(id)sender;
- (IBAction)PlaySadTrombone:(id)sender;
- (IBAction)PlayBadJokeSound:(id)sender;
- (IBAction)PlayHorrorSynth:(id)sender;
- (IBAction)PlayLKWPeep:(id)sender;
- (IBAction)PlayUiToll:(id)sender;
- (IBAction)StopSound:(id)sender;



AVAudioPlayer *theAudio;



@end


//PlaySound Function
void playSound(NSString *filename,NSString *type){
    [theAudio stop];

    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:type];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];


    [theAudio play];




}

.m file: i want to call it like this

- (IBAction)StopSound:(id)sender {
    [theAudio stop];


}

why is the "theAudio" variable undeclared in the StopSound() function???

please help me :(

thx in advance

EDIT: IT WORKS NOW LIKE I DID IT ABOVE

THX!!!

Upvotes: 0

Views: 819

Answers (1)

Anomie
Anomie

Reputation: 94854

Because StopSound is not a method of a class, it's a C-style function. Why don't you just get rid of the C-style function and put the code directly into - (IBAction)StopSound:(id)sender?

Upvotes: 1

Related Questions