iPhone子类的声音

时间:2011-08-27 04:00:37

标签: iphone subclass avaudioplayer nsobject audio

更新!固定!这现在有效!随意使用!

#import <Foundation/Foundation.h>
#import "AVFoundation/AVAudioPlayer.h"

@interface SoundsClass : NSObject <AVAudioPlayerDelegate> {


AVAudioPlayer *buttonClick;
AVAudioPlayer *slide;
AVAudioPlayer *Cellpush;
AVAudioPlayer *pop;
AVAudioPlayer *tick;
AVAudioPlayer *done;
AVAudioPlayer *swoosh;


}


@property (nonatomic, retain) AVAudioPlayer *buttonClick;
@property (nonatomic, retain) AVAudioPlayer *slide;
@property (nonatomic, retain) AVAudioPlayer *Cellpush;
@property (nonatomic, retain) AVAudioPlayer *pop;
@property (nonatomic, retain) AVAudioPlayer *tick; 
@property (nonatomic, retain) AVAudioPlayer *done;
@property (nonatomic, retain) AVAudioPlayer *swoosh;




-(void)PlayButtonClick:(id)sender;

-(void)PlaySlide:(id)sender;

-(void)PlayCellPush:(id)sender;

-(void)PlayPop:(id)sender;

-(void)PlayTick:(id)sender;

-(void)PlayDone:(id)sender;

- (无效)PlaySwoosh:(ID)发送者;

 @end




#import "SoundsClass.h"


@implementation SoundsClass

 @synthesize buttonClick, swoosh, slide, tick, done, Cellpush, pop;


-(void)dealloc{


[buttonClick release];
[swoosh release];
[slide release];
[tick release];
[done release];
[Cellpush release];
[pop release];
[super dealloc];
}




-(void)PlayButtonClick:(id)sender{


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"button_click"    
ofType:@"caf"];
    self.buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
fileURLWithPath:path] error:NULL];
    self.buttonClick.delegate = self;
    [self.buttonClick play];  

}    




}



-(void)PlaySlide:(id)sender{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"popover_show" o   
fType:@"caf"];
    slide = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]   
error:NULL];
    slide.delegate = self;
    [slide play];  

}       



}



-(void)PlayCellPush:(id)sender{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"cell_swoosh"   
ofType:@"caf"];
    buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
fileURLWithPath:path] error:NULL];
    buttonClick.delegate = self;
    [buttonClick play];  

}       



}



-(void)PlayPop:(id)sender{


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"pop" ofType:@"caf"];
    buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL   
fileURLWithPath:path] error:NULL];
    buttonClick.delegate = self;
    [buttonClick play];  

}       


}



-(void)PlayTick:(id)sender{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Tick" ofType:@"caf"];
    buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL  
fileURLWithPath:path] error:NULL];
    buttonClick.delegate = self;
    [buttonClick play];  

}       

}



-(void)PlayDone:(id)sender{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Done" ofType:@"caf"];
    buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL   
fileURLWithPath:path] error:NULL];
    buttonClick.delegate = self;
    [buttonClick play];  

}       


}




-(void)PlaySwoosh:(id)sender{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"appSounds"]) {    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"flip" ofType:@"caf"];
    buttonClick = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL 
fileURLWithPath:path] error:NULL];
    buttonClick.delegate = self;
    [buttonClick play];  

}       



}


@end

如果您想使用该课程

#import "SoundsClass.h"

 @class SoundsClass;  

 SoundsClass *sounds;

 @property (nonatomic, retain) SoundsClass *sounds;

-(IBAction)openFolder:(id)sender;



-(IBAction)openFolder:(id)sender{


sounds = [[SoundsClass alloc] init];

[self.sounds PlayButtonClick:sender];

}

这很简单,但使用默认值只需使用开关并设置bool

0 个答案:

没有答案