iphone - AVAudioPlayer 没有声音输出

标签 iphone objective-c audio

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

- (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"];

但是我得到的声音为零。

最佳答案

更新的答案:

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;

关于iphone - AVAudioPlayer 没有声音输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10856332/

相关文章:

iphone - 为什么我的应用程序崩溃了? UITable 和 Controller

reactjs - 播放未导入的src的音频

javascript - 单击按钮后未调用Web Audio ScriptNode

iphone - 问题 : subview becoms on top of superview when it moves out of the superview

iphone - 当超过特定阈值时使 UIPanGestureRecognizer 拉伸(stretch)

ios - 在我的应用程序打开文件后,保留对该文件的引用,以便我稍后可以再次打开它

ios - 工作几天后,UITextField.inputView 突然不弹出 UIPickerView

iphone - MKMapView 框架在 MKMapType 声明中没有 relief google-map 类型,但它可以工作

iOS - 过滤后的声音停止后可听到爆音

iphone - SIGABRT 在 NSManagedObjectContext = [NSEntityDescription ...] 之后