0

这是最简单的 AVAudioPlayer 代码,只是无法播放任何内容。没有错误,控制台中什么都没有,文件肯定被找到,就好像我将 URL 字符串更改为不存在的东西一样,我确实遇到了崩溃。我在这里做错了什么?我在有和没有委托的情况下,以及有和没有prepareToPlay的情况下都试过了,但我什么也做不了。我也尝试过各种声音文件。真的把我的头发扯掉了!

@implementation ViewController

- (IBAction)playSound:(id)sender
{
    NSURL *soundLocation = [[NSURL alloc] initWithString:@"Lot01.wav"];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundLocation error:nil];
    [audioPlayer setDelegate:self];
    [audioPlayer play];
}

@end
4

2 回答 2

6

原来这是 ARC 发布的问题,我通过为有问题的 AVAudioPlayer 添加 @property 和 @synthesize 对来修复它,并将其声明为强大。它摆脱了所有这些错误,并且毫无问题地播放了文件。

于 2012-02-21T15:41:30.957 回答
1

似乎它没有正确获取文件的路径,也许可以尝试这样的事情。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Lot01" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:filePath];

然后完全按照你的方式初始化 AVAudioPlayer,除非在这种情况下它是 withContentsOfURL:url。

于 2012-02-20T14:28:46.650 回答