在iOS5中播放wav文件

时间:2012-06-20 10:56:25

标签: iphone objective-c cocoa-touch avaudioplayer

我一直有一些关于wav文件最近没玩的问题,我使用的是我两年前编写的代码(为ARC调整过),我很好奇,如果我这样做是最好的方式,因为iOS已经改变了从那以后呢?

@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *buttonAudioPlayer;
@end

@implementation ViewController
@synthesize buttonAudioPlayer;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSBundle *bundle = [NSBundle mainBundle];
    NSError *error = nil;

    NSURL *soundURL = [NSURL fileURLWithPath:[bundle pathForResource:@"buttonClick_002" ofType:@"wav"]];
    [self setButtonAudioPlayer:[[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]];
    [buttonAudioPlayer prepareToPlay];
}

- (IBAction)buttonPressed {
    NSLog(@"Button Pressed ...");
    [[self buttonAudioPlayer] play];
}

0 个答案:

没有答案