AVAudioPlayer拒绝播放任何内容,但没有错误等

发布于 2025-01-06 22:49:30 字数 525 浏览 0 评论 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2025-01-13 22:49:30

结果发现这是 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.

葬シ愛 2025-01-13 22:49:30

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

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

然后按照你原来的方式初始化 AVAudioPlayer ,除了在这种情况下它会是 withContentsOfURL:url 。

It seems like it's not correctly getting the path to your file, maybe try something like this.

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

then init the AVAudioPlayer exactly how you were except in that case it'd be withContentsOfURL:url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文