访问麦克风后整个应用程序的音量变得更安静
Posted
技术标签:
【中文标题】访问麦克风后整个应用程序的音量变得更安静【英文标题】:Volume of entire app gets quieter after accessing mic 【发布时间】:2016-04-14 16:45:02 【问题描述】:我的 ios 应用程序出现音量问题。当我拨打setupMic()
时,整个应用程序的音量显着降低。
这是我正在使用的代码:
func setupMic()
//make an AudioSession, set it to PlayAndRecord and make it active
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
do
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
catch
print("There was an error setting the category")
do
try audioSession.setActive(true)
catch
print("There was an error setting the audio session to active")
//set up the URL for the audio file
let documents: AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let str = documents.stringByAppendingPathComponent("recordTest.caf")
let url = NSURL.fileURLWithPath(str as String)
//make a dictionary to hold the recording setting so we can instantiate our AVAudioRecorder
let number = NSNumber(unsignedInt: kAudioFormatAppleIMA4)
let recordSettings: [String: AnyObject] = [AVFormatIDKey: number,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
AVEncoderBitRateKey: 12800,
AVLinearPCMBitDepthKey: 16,
AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue]
//Instantiate an AVAudioRecorder
do
recorder = try AVAudioRecorder(URL: url, settings: recordSettings)
catch
print("There was an error")
//This function is called everytime our timer levelTimer fires
func levelTimerCallback()
recorder.updateMeters()
let averagePower = self.recorder.peakPowerForChannel(0)
if averagePower > -7
stopMonitoring()
print(recorder.peakPowerForChannel(0))
didCompleteChallenge(true)
func startMonitoring()
if self.recorder != nil
recorder.prepareToRecord()
recorder.meteringEnabled = true
//start recording
recorder.record()
//instantiate a timer to be called with whatever frequency we want to grab metering values
self.levelTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(levelTimerCallback), userInfo: nil, repeats: true)
func stopMonitoring()
self.recorder.stop()
self.recorder.deleteRecording()
self.levelTimer.invalidate()
我在updateWith()
方法中调用setupMic()
和startMonitoring()
。当视图再次使用updateWith()
更新时,我也会调用stopMonitoring()
访问麦克风后,音量会降低。有什么建议么?有什么修复吗?
【问题讨论】:
【参考方案1】:解决了这个问题。音量实际上并没有变小,音频实际上是通过听筒传输的。我只需要将音频会话类别选项设置为 .DefaultToSpeaker。 AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [.DefaultToSpeaker])
。无论如何感谢互联网。
【讨论】:
我设置了这个类别,但是当我插入耳机时,我知道只有一侧有声音,另一侧不工作,有什么线索吗?以上是关于访问麦克风后整个应用程序的音量变得更安静的主要内容,如果未能解决你的问题,请参考以下文章