AVAudioPlayer通过扬声器 [英] AVAudioPlayer via Speakers

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

问题描述

我得到了以下代码:

- (id)init {
    if (self = [super init]) {
        UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
        AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);    

        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

        [[AVAudioSession sharedInstance] setDelegate:self];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
    }
    return self;
}

但不知何故声音不想从扬声器中传出来,有人可以吗看看我做错了什么?

But somehow the sound does not want to come out of the speakers, can someone see what I am doing wrong?

我用来玩的代码是:

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFilePathURL error:nil];
[player prepareToPlay];
[player setVolume:1.0];
[player play];


推荐答案

我使用了AudioToolbox框架,这就是我初始化音频的原因会话如下:

I used the AudioToolbox framework that's why I initialized my audio session as following:

AudioSessionInitialize(NULL, NULL, NULL, NULL);

这是我用来配置音频会话的其余代码。我没有覆盖音频路线,我也认为没有必要。

Here's the rest of my code that I used to configure the audio session. I didn't override the audio route and I also think this is not necessary.

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
OSStatus err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                       sizeof(sessionCategory),
                                       &sessionCategory);
AudioSessionSetActive(TRUE);
if (err) {
    NSLog(@"AudioSessionSetProperty kAudioSessionProperty_AudioCategory failed: %d", err);
}

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

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