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

时间:2012-11-12 08:15:16

标签: 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];

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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