首页 > 代码库 > Swift听筒模式和扬声器模式切换

Swift听筒模式和扬声器模式切换


 本文只作自用笔记,不喜忽喷,诚谢纠错.

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3495af } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } span.s1 { } span.s2 { color: #000000 } span.s3 { color: #ed462f } span.s4 { color: #008f00 } span.s5 { font: 14.0px "PingFang SC"; color: #008f00 } span.s6 { color: #3495af }</style>

听筒模式

UIDevice.current.isProximityMonitoringEnabled = true //开启红外线功能

        let session = AVAudioSession()

        do{

            try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [])

            try session.setActive(true)

        }catch{

            

        }

扬声器模式

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3495af } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } span.s1 { } span.s2 { color: #000000 } span.s3 { color: #ed462f } span.s4 { color: #008f00 } span.s5 { font: 14.0px "PingFang SC"; color: #008f00 } span.s6 { color: #3495af }</style>

UIDevice.current.isProximityMonitoringEnabled = true //开启红外线功能

        let session = AVAudioSession()

        do{

            try session.setCategory(AVAudioSessionCategoryPlayback, with: [])

            try session.setActive(true)

        }catch{

            

        }

Swift听筒模式和扬声器模式切换