2017-03-08 13 views
8

मेरे ऐप में मैं AVAudio PortTypes को पहले से ही फोन से कनेक्ट करना चाहता हूं। नीचे दिया गया कोड ब्लूटूथए 2 डीडी और हेडफ़ोन के लिए काम करता है, लेकिन जब मैं फोन को अपनी कार हैंड्सफ्री से कनेक्ट करता हूं तो ब्लूटूथ एचएफपी नहीं। क्या कोई मेरी मदद कर सकता है?! मुझे लगता है कि हैंड्सफ्री/एवी/ब्लूटूथ, और कई अन्य सभी एसओ पदों के माध्यम से किया गया है, लेकिन यह काम नहीं कर सकता है कि यह ब्लूटूथ एचएफपी आउटपुट पोर्ट टाइप को क्यों पहचान नहीं रहा है।ऐप AVAudio ब्लूटूथ एचएफपी हैंड्सफ्री पोर्ट स्विफ्ट

import AVFoundation 

func startCheckAVConnection() { 

    // set the AVAudioSession to allow bluetooth. This do/try/catch doesn't seem to make a difference if it is here or not. 
    do{ 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) 
    } catch{ 
     print(error) 
    } 

    // Check possible outputs for handsfree 
    let outputs = AVAudioSession.sharedInstance().currentRoute.outputs 

    if outputs.count != 0 { 
     for output in outputs { 
      if output.portType == AVAudioSessionPortBluetoothA2DP { 
       peripheralLabel.text = "connected to BluetoothA2DP" 
      } else if output.portType == AVAudioSessionPortBluetoothHFP { // NOT RECOGNISED 
       peripheralLabel.text = "connected to BluetoothHFP" 
      } else if output.portType == AVAudioSessionPortHeadphones { 
       peripheralLabel.text = "connected to Headphones" 
      } 
     } 
    } else { 
     peripheralLabel.text = "Please connect handsfree" 
    } 

    // Add observer for audioRouteChangeListener 
    NotificationCenter.default.addObserver(
     self, 
     selector: #selector(TopVC.audioRouteChangeListener(_:)), 
     name: NSNotification.Name.AVAudioSessionRouteChange, 
     object: nil) 
} 
+0

आपको उस मामले में 'output.portType' के लिए क्या मूल्य मिलता है? आपके पास कितने आउटपुट हैं? क्या यह हो सकता है कि आपके पास विभिन्न हैं, और 'peripheralLabel.text' किसी अन्य मान के साथ ओवरराइट हो जाएं? – Larme

+0

हाय लार्मे, केवल एक ही है .portType जब यह A2DP या हेडफ़ोन उठाता है, लेकिन यह ब्लूटूथ एचएफपी को कभी नहीं चुनता है। कोई और विचार? – richc

उत्तर

0

मुझे setCategory के बाद setActive जोड़ने की आवश्यकता है।

do { try AVAudioSession.sharedInstance().setActive(true) 
     print("setActive") 
    } catch { 
     print(error) 
    } 
0

करने के लिए अपने वर्ग सेटअप बदलने का प्रयास करें:

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) 

HFP बहु मार्ग श्रेणी AFAICT साथ काम नहीं करता।

+0

हाय ऑस्टिनो, धन्यवाद मैंने PlayAndRecord की कोशिश की और अभी भी एचएफपी नहीं उठा रहा है। यह ए 2 डीडी जुर्माना उठाता है, लेकिन एचएफपी नहीं। कोई और विचार? – richc

+0

मुझे SetCategory के बाद SetActive जोड़ने की भी आवश्यकता है। अब ठीक काम करता है। {प्रयास करें AVAudioSession.sharedInstance()। setActive (true) प्रिंट ("setActive") } पकड़ें { प्रिंट (त्रुटि) } – richc

संबंधित मुद्दे