iOS uygulamamda bir birim sorunu yaşıyorum. setupMic()
numaralı telefonu aradığımda, uygulamanın tümünün ses seviyesi önemli ölçüde azaldı.Tüm uygulamaların sesi, mikrofon erişimine eriştikten sonra daha sessiz hale geliyor
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()
}
Bir updateWith()
yönteminde setupMic()
ve startMonitoring()
çağrı:
İşte kullanıyorum kodudur. Görünüm updateWith()
ile yeniden güncellendiğinde de stopMonitoring()
'u da arıyorum. Mikrofon erişildiğinde ses azalır. Baska öneri? Herhangi bir düzeltme var mı?
Bu kategoriyi ayarlıyorum, fakat kulaklıklarımı bağladığımda, sadece bir tarafın ses aldığını, diğer tarafın çalışmadığını, herhangi bir ipucu olduğunu anlamalı mıyım? –
Teşekkürler @Cameron, yukarı oy, benim için çalıştı –