2011-03-02 105 views
0

希望你能幫助我(試過很多東西在這裏,但沒有奏效)AVAudioPlayer停止聲音iPhone SDK目標C

我想通過按下一個按鈕來播放的幾種聲音,我想一個按鈕取消播放聲音。

頭:

#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文件: 我想這樣稱呼它

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


} 

爲什麼是 「theAudio」 變量未申報的StopSound()函數???

請幫我:(

THX提前

編輯:現在的工作,像我一樣在它上面

THX !!!

回答

1

因爲StopSound不是的方法類,它是一個C風格的函數,爲什麼不只是擺脫了C風格的功能,並把代碼直接放到這樣的- (IBAction)StopSound:(id)sender

+0

?(IBAction)StopSound:(id)發送者{ \t [theAudio stop]; \t } – Chris 2011-03-02 11:58:38

+0

編輯它,如果你希望能夠使用theAudio不工作,要麼:( – Chris 2011-03-02 11:59:35

+0

,你必須存儲在某個地方,比如在你的類的實例變量。現在你只是把它PlayAudio函數內部的局部變量,所以在函數返回後無法訪問(並泄露)。 – Anomie 2011-03-02 12:07:53