gpt4 book ai didi

ios - AVAudioSession : Playing audio through earpiece speaker

转载 作者:行者123 更新时间:2023-12-01 15:53:26 25 4
gpt4 key购买 nike

我正在尝试通过听筒扬声器播放音频并且它工作正常。相同的代码在以下情况下不起作用。

  1. 打开相机录制视频
  2. 不是开始录制,而是取消它
  3. 然后,尝试通过听筒播放音频无效。正在通过主扬声器播放

这是我通过耳机播放音频的代码。

-(void)initialAVaudioPlayer
{
if (player==nil) {
NSError *error = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[session setActive: YES error:nil];

AVAudioSessionPortDescription *routePort = session.currentRoute.outputs.firstObject;
NSString *portType = routePort.portType;

if ([portType isEqualToString:@"Receiver"]) {
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
} else {
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
}


NSString *path;
NSError *err;
NSString *name;
name = @"referenceaudio";
path = [[NSBundle mainBundle] pathForResource:name ofType:@"wav"];
if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&err];
if (!err) {
player.numberOfLoops = -1;

}
else{
//NSLog(@"%@",[err description]);
}

}
}
}

这是代码,当用户按下取消时,

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(@"here");
[self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

这是使 AVAudioSession 路由到耳机扬声器(内置扬声器)或底部扬声器的解决方案,适用于 Swift 4.2、iOS 12、SDK 11:

1) 在您的项目中创建一个名为 AudioSession 的文件并实现这 3 个函数。

import AVFoundation
import UIKit

let audioSession = AVAudioSession.sharedInstance()

func configureAudioSessionCategory() {
print("Configuring audio session")
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.voiceChat)
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
print("AVAudio Session out options: ", audioSession.currentRoute)
print("Successfully configured audio session.")
} catch (let error) {
print("Error while configuring audio session: \(error)")
}
}

func configureAudioSessionToSpeaker(){
do {
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
try audioSession.setActive(true)
print("Successfully configured audio session (SPEAKER-Bottom).", "\nCurrent audio route: ",audioSession.currentRoute.outputs)
} catch let error as NSError {
print("#configureAudioSessionToSpeaker Error \(error.localizedDescription)")
}
}

func configureAudioSessionToEarSpeaker(){

let audioSession:AVAudioSession = AVAudioSession.sharedInstance()
do { ///Audio Session: Set on Speaker
try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
try audioSession.setActive(true)

print("Successfully configured audio session (EAR-Speaker).", "\nCurrent audio route: ",audioSession.currentRoute.outputs)
}
catch{
print("#configureAudioSessionToEarSpeaker Error \(error.localizedDescription)")
}
}

2) 在您当前的 View Controller viewWillAppear 中播放音频,调用:

    configureAudioSessionCategory()

3) 如果你想把音频放在扬声器上,调用函数:

    configureAudioSessionToSpeaker()

如果你想把音频放在麦克风上,调用函数:

    configureAudioSessionToEarSpeaker()

*我会制作一些标签,因为它是 swift 4.2 的新标签,而且当您搜索该主题时很难(或找不到)在 google 或此处找到它。

标签:avaudio session to ear speaker, audio route to top speaker, earpiece speaker, default speaker, bottom speaker, loud speaker.

关于ios - AVAudioSession : Playing audio through earpiece speaker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com