ios - 将声音输出到接收器而不是扬声器

标签 ios avaudioplayer avaudiosession

我的音频从扬声器播放,我希望它从接收器播放(使用按钮)。 起初我试图强制声音从接收器输出。 (我要兼容iOS 3.2)

这是 [super viewDidLoad] 之后的结果;

  [[AVAudioSession sharedInstance] setDelegate: self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];

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

  [[AVAudioSession sharedInstance] setActive:YES error:nil];

稍后当我调用该剧时:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:lblText.text
                                         ofType:@"mp3"]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@", 
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;

    }

    [self playAudio];

    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:[volumeSlider frame]] autorelease];
    [volumeSlider removeFromSuperview];
    [self.view addSubview:volumeView];

我做错了什么?

最佳答案

答案如下: 覆盖仅适用于 kAudioSessionCategory_PlayAndRecord 类别,不适用于 AVAudioSessionCategoryPlayback。

https://developer.apple.com/library/ios/#qa/qa1754/_index.html

关于ios - 将声音输出到接收器而不是扬声器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340252/

相关文章:

ios - 应用内购买 - 购买成功但交易失败

ios - 如何知道使用 SDWebImage 库已经下载了特定的 url 图像?

swift - 是否可以录制来自 iPhone 的音频?

ios - iOS音量仅在播放时可调

ios - 音频播放器在后台播放,应该基于硬件静音开关工作

ios - 我有 2 个数组。如果在两个数组中都找到,则需要从其中一个数组中删除字符串

ios - 如何随机化我的 ImageView ?

ios - 如何在iOS应用程序中循环连续播放声音文件?

ios - 使用 AVAudioPlayer 在 iOS 中自动平移

ios - 第一次音频启动延迟 - ios Swift