AVAudioPlayer 没有声音输出 [英] AVAudioPlayer gives no sound output

查看:31
本文介绍了AVAudioPlayer 没有声音输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将音频工具箱和 avfoundation 导入我的班级,并将框架添加到我的项目中,并使用此代码播放声音:

I have imported audio toolbox and avfoundation to my class and added the frameworks to my project and am using this code to play a sound:

- (void)playSound:(NSString *)name withExtension:(NSString *)extension
{
    NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:extension]]; 
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    audioPlayer.delegate = self;
    audioPlayer.volume = 1.0;
    [audioPlayer play];
}

我这样称呼它:

[self playSound:@"wrong" withExtension:@"wav"];

但是我的声音为零.

推荐答案

更新答案:

我遇到了这个问题.它与ARC无关准备播放.只需对它进行强引用,它就会起作用.

I've got the issue. It's with ARC and has nothing to do with prepareForPlay. Simply make a strong reference to it and it will work.

如下用户所述:Kinderchocolate :)

在代码中:

.h
~
@property (nonatomic, strong) AVAudioPlayer *player;


.m
~
@implementation
@synthesize _player = player;

这篇关于AVAudioPlayer 没有声音输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆