【问题标题】:AVAudioPlayer dose not play sound, gives errorAVAudioPlayer 不播放声音,给出错误
【发布时间】:2013-11-20 02:07:04
【问题描述】:

我正在尝试播放 MP3 文件,但是我的代码有问题。这就是我所拥有的:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = -1;

        if (!audioPlayer) {
            NSLog(@"localizedDescription : %@", [error localizedDescription]);
        } else {
            [audioPlayer play];
        }

这是我收到的错误

localizedDescription : The operation couldn’t be completed. (OSStatus error -43.)

如何阻止这种情况发生?我想播放我的声音一次然后停止它,但它甚至无法启动。

【问题讨论】:

    标签: ios avaudioplayer


    【解决方案1】:

    问题可能是路径无效引起的。

    尝试如下构建它

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"audiofile" ofType:@"mp3"];
    NSURL *soundURL = [NSURL URLWithString:soundPath];
    

    改为。

    还要确保audiofile.mp3 包含在当前目标中。

    【讨论】:

    • 好吧这种作品。我没有给出错误,它进入 if 语句的 else 部分。但是我仍然没有听到任何声音,我已经检查过了,声音是绝对开启的,并且没有静音..
    • 您可能尚未配置音频会话。检查这个问题:stackoverflow.com/questions/12532290/…
    • 这是构造 URL 的错误方法。改用+fileURLWithPath:,甚至更好,-[NSBundle URLForResource:…]
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多