AVAudioPlayer拒绝播放任何内容,但没有错误等
这是最简单的 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
This is the simplest possible AVAudioPlayer code, and it's just failing to play anything back. No errors, nothing in the console at all, the file is definitely being found as if I change the URL string to something which isn't there, I do get a crash. What am I doing wrong here? I've tried it with and without the delegate, as well as with and without prepareToPlay, and I can't get anything out. I've tried various sound files, too. Really tearing my hair out on this one!
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果发现这是 ARC 发布的问题,我通过为有问题的 AVAudioPlayer 添加 @property 和 @synthesize 对来修复它,并将其声明为强。它消除了所有这些错误,并且播放文件没有任何问题。
Turned out it was an issue with ARC releasing, I fixed it by adding a @property and @synthesize pair for the AVAudioPlayer in question, and declaring it as strong. It got rid of all of these errors, and played the file with no problems.
似乎没有正确获取文件的路径,也许可以尝试这样的操作。
然后按照你原来的方式初始化 AVAudioPlayer ,除了在这种情况下它会是 withContentsOfURL:url 。
It seems like it's not correctly getting the path to your file, maybe try something like this.
then init the AVAudioPlayer exactly how you were except in that case it'd be withContentsOfURL:url.