我正在使用AVAudioRecorder录制音频,如How do I record audio on iPhone with AVAudioRecorder?
所示然后我使用AVAudioPlayer播放录音。然而,声音是从耳机发出的,而不是扬声器。我如何将声音重定向到扬声器?
TIA!
答案 0 :(得分:27)
我意识到这个问题相当陈旧,但当我遇到同样的问题时,我发现了一个简单的解决方案,希望能帮助其他人寻求使用更响亮的媒体扬声器而不是接收器扬声器。我使用的方法是使用AVAudioSessionCategoryOptions中的DefaultToSpeaker选项设置音频会话:
在Swift中(假设您的音频会话名为session
) -
session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker, error: nil)
在Obj-C中 -
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
答案 1 :(得分:18)
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
答案 2 :(得分:14)
Swift 3
在录制我录制的声音之前:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
在播放之前我设置了:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
目标C
记录前:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error:nil];
播放前:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
答案 3 :(得分:11)
这对我来说非常适合Swift2
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
答案 4 :(得分:6)
这是一个老问题,但是另一个答案对我没有帮助......但是,我发现了一个解决方案,我将其发布以供将来参考,以防有人(或我未来!)需要它。
以下博文中介绍了该解决方案:iOS: Force audio output to speakers while headphones are plugged in 。
您需要在项目中创建新的Objective-C类AudioRouter。然后将AudioRouter.h
导入到您要启动音频功能的类的头文件中。接下来,在相应的.m
文件中,在viewDidLoad
方法中添加以下行:
AudioRouter *foobar = [[AudioRouter alloc] init];
[foobar initAudioSessionRouting];
[foobar forceOutputToBuiltInSpeakers];
现在你有音频(例如AVAudioPlayer)输出强制扬声器!请注意,如果您在应用程序运行时插入耳机 ,则所有音频输出都会定向到耳机。
答案 5 :(得分:6)
<强> Swift2:强>
let byte: u8 = io::stdin().bytes().next().unwrap();
答案 6 :(得分:1)
Swift 3
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("Audio Session error: \(error.localizedDescription)")
}
答案 7 :(得分:1)
回答Swift 4.0
func SetSessionPlayerOn()
{
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch _ {
}
}
func SetSessionPlayerOff()
{
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch _ {
}
}
答案 8 :(得分:1)
这是在扬声器中而不是在麦克风中输出auido的关键线。请注意,应在录制时设置
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
以下是完成录制功能的功能
private func setupRecorder() {
if isAudioRecordingGranted {
let session = AVAudioSession.sharedInstance()
do {
if #available(iOS 10.0, *) {
try! session.setCategory(.playAndRecord, mode:.spokenAudio)
}
else {
// Workaround until https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949 isn't fixed
session.perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
}
try session.setActive(true)
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
]
audioRecorder = try AVAudioRecorder(url: fileUrl(), settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
self.recorderState = .Ready
}
catch let error {
recorderState = .error(error)
}
}
else {
recorderState = .Failed("Don't have access to use your microphone.")
}
}
答案 9 :(得分:1)
雨燕5
// Get the singleton instance.
let recordingSession = AVAudioSession.sharedInstance()
do {
// Set the audio session category, mode, and options.
try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker])
try recordingSession.setActive(true)
} catch {
print("Failed to set audio session category.")
}
检查Developer Documentation以获得有关 AVAudioSession.CategoryOptions 的更多信息。
答案 10 :(得分:0)
Swift 5:要在蓝牙设备和默认扬声器上工作,请使用:
let recordingSession = AVAudioSession.sharedInstance()
try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])