0

我最近遇到了一些关于 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];
}
4

0 回答 0