IOS:AVAudioSession无法正常工作

IOS:AVAudioSession无法正常工作,第1张

概述我试图使用AVAudioSession,但它抛出了这个运行时错误: [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:Unknown选择端口扬声器的数据源(类型扬声器).如果它有助于我只是记录音频,并监控当前的分贝.我将类别设置为AVAudioSessionCategoryRecord,将模式设置为AVAudioSessionMo 我试图使用AVAudioSession,但它抛出了这个运行时错误:
[avas] AVAudioSessionPortImpl.mm:56:ValIDaterequiredFIElds:UnkNown选择端口扬声器的数据源(类型扬声器).如果它有助于我只是记录音频,并监控当前的分贝.我将类别设置为AVAudioSessioncategoryRecord,将模式设置为AVAudioSessionModeMeasurement.
这是代码:
class VIEwController: UIVIEwController {    let captureSession = AVCaptureSession()    var recording = false;    var ready = false;    let audioSession = AVAudioSession.sharedInstance()    @IBOutlet public weak var dBLabel: UILabel!    func alert(Title: String,message: String = "",handler: ((UIAlertAction) -> Swift.VoID)? = nil) -> VoID {        var usedMessage: String        if(message.characters.count < 1) {            usedMessage = Title;        } else {            usedMessage = message;        }        let alert = UIAlertController(Title: Title,message: usedMessage,preferredStyle: .alert)        alert.addAction(UIAlertAction(Title: NSLocalizedString("OK",comment: "Default action"),style: .default,handler: handler))        self.present(alert,animated: true,completion: nil)    }    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()    }    func checkPermission()    {        switch AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeAudio)        {        case .authorized:            NSLog("Authorized for Microphone Use")        case .notDetermined:            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio,completionHandler: { granted in                self.checkPermission()            })        case .denIEd:            let alert = UIAlertController(Title: "DenIEd Access to Microphone",message: "You denIEd access to the microphone,please enable access in settings",preferredStyle: .alert)            alert.addAction(UIAlertAction(Title: "Go to Settings",handler: { _ in                guard let settingsUrl = URL(string: UIApplicationopenSettingsURLString) else {                    return                }                if UIApplication.shared.canopenURL(settingsUrl) {                    UIApplication.shared.open(settingsUrl,completionHandler: { (success) in                    })                }            }))            alert.addAction(UIAlertAction(Title: NSLocalizedString("Cancel",comment: "Cancel"),style: .cancel,handler: nil))            self.present(alert,completion: nil)        case .restricted:            alert(Title: "Restricted",message: "You cannot enable the microphone,so you cannot use the app",handler: { _ in                NSLog("The \"OK\" alert occured.")            })        }    }    @IBAction func toggleRecord(_ sender: UIbutton) {        if(!ready)        {return}        NSLog("Toggled Recording")        recording = !recording;        if(recording)        {            sender.setimage(UIImage(named: "MicIconHighlighted.png"),for: .normal)            sender.setimage(UIImage(named: "MicIconHighlightedSelected.png"),for: .highlighted)            //captureSession.startRunning()            do            {                try audioSession.setActive(true)            } catch {                NSLog("Activating AudioSession Failed")            }        } else {            sender.setimage(UIImage(named: "MicIcon.png"),for: .normal)            sender.setimage(UIImage(named: "MicIconSelected.png"),for: .highlighted)            //captureSession.stopRunning()            do            {                try audioSession.setActive(false)            } catch {                NSLog("Deactivating AudioSession Failed")            }        }    }    overrIDe func vIEwDIDAppear(_ animated: Bool) {        super.vIEwDIDAppear(animated)        checkPermission()        do        {            try audioSession.setcategory(AVAudioSessioncategoryPlayAndRecord)        } catch {            NSLog("Setting category on AudioSession Failed")        }        do        {            try audioSession.setMode(AVAudioSessionModeMeasurement)        } catch {            NSLog("Setting mode on AudioSession Failed")        }        do        {            try audioSession.overrIDeOutputAudioPort(AVAudioSessionPortOverrIDe.speaker)        } catch {            NSLog("Failed Setting Audio Output Data Source")        }        //NSLog("deBUG info: \(audioSession.outputDataSources!.count)");        /*captureSession.beginConfiguration()        let audioDeviceinput: AVCaptureDeviceinput        let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)        if(audioDevice != nil && (audioDevice?.isConnected)!) {            NSLog("Audio Device name: \(audioDevice!.localizedname)")        } else {            NSLog("AVCapture Device default audio device Failed or device not connected")        }        do {            audioDeviceinput = try AVCaptureDeviceinput(device: audioDevice)        } catch {            alert(Title: "Failed to create Capture Device",message: "Failed to create Capture Device",handler: nil)            return        }        if(captureSession.canAddinput(audioDeviceinput))        {            captureSession.addinput(audioDeviceinput)        } else {            alert(Title: "Failed to Add input",message: "Failed to add Audio input Device",handler: nil)        }        let audioOutput = AVCaptureAudioDataOutput()        var audioRecorder = AVAudioRecorder()        audioRecorder.        var audioQueue = dispatchQueue(label: "audioqueue",attributes: .concurrent)        audioOutput.setSampleBufferDelegate(AudioOutputSampleBufferDelegate(vc: self),queue: audioQueue)        NSLog("Current Queue: \(audioOutput.sampleBufferCallbackQueue.description)")        if(captureSession.canAddOutput(audioOutput))        {            captureSession.addOutput(audioOutput)            captureSession.commitConfiguration()        } else {        alert(Title: "Failed to Add Output",message: "Failed to add Audio Output Device",handler: nil)        }*/        ready = true    }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }}
解决方法 AVAudioSession在Swift 4.2中有一些变化

在vIEwDIDAppear()下,试试这个:

// Prepare Audio Session    self.audioSession = AVAudioSession.sharedInstance()    try audioSession.setcategory(AVAudioSession.category.playAndRecord,mode: .measurement,options: .defaultToSpeaker)    try audioSession.setActive(true,options: .notifyOthersOnDeactivation)

当我在Xcode 10中将语言转换为Swift 4.2时,它对我有用
你可以通过去…来做到这一点

Edit,Convert,To Current Swift Syntax…

总结

以上是内存溢出为你收集整理的IOS:AVAudioSession无法正常工作全部内容,希望文章能够帮你解决IOS:AVAudioSession无法正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1103013.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存