【发布时间】:2011-07-04 05:38:13
【问题描述】:
我正在开发一个 iOS 应用程序,我应该能够在其中录制声音,将其保存到文件系统,然后从磁盘播放。当我尝试从磁盘播放文件时,我几乎听不到任何声音。音量太低。但我很确定我会将设备音量设置为最大。我正在使用AVAudioRecorder 和 AVAudioPlayer` 进行录制和播放。
有人可以指出可能是什么问题吗?
【问题讨论】:
标签: iphone avaudioplayer avaudiorecorder
我正在开发一个 iOS 应用程序,我应该能够在其中录制声音,将其保存到文件系统,然后从磁盘播放。当我尝试从磁盘播放文件时,我几乎听不到任何声音。音量太低。但我很确定我会将设备音量设置为最大。我正在使用AVAudioRecorder 和 AVAudioPlayer` 进行录制和播放。
有人可以指出可能是什么问题吗?
【问题讨论】:
标签: iphone avaudioplayer avaudiorecorder
对于大多数情况,这里的解决方案可能是使用带有AVAudioSessionCategoryPlayAndRecord 类别的 .defaultToSpeaker 选项:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker])
但对我来说,问题是我的应用程序中的另一个组件在音频会话上设置了AVAudioSessionModeMeasurement 模式,无论出于何种原因,这都会降低输出音量。
【讨论】:
使用该方法设置AVAudioSession的类别
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
【讨论】:
试试这个代码。我认为 setMode 解决了我的问题。
NSError * outError = nil;
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
[ session overrideOutputAudioPort: AVAudioSessionPortOverrideSpeaker error:&outError];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setMode:AVAudioSessionModeSpokenAudio error:nil];
[session setActive:YES error:nil];
if(outError){
NSLog(@"speak error %@", outError);
}
【讨论】: