2011-03-02 108 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