2017-08-17 11 views
1

ユーザーがアクションシートでオプションを選択したときにデバイスへのオーディオ出力を変更しようとしています。これは私がもう少し効率的にこれを行うことができるかもしれないが、これは何であると私はデバイスに行くのオーディオのために選択した場合、Bluetoothは次回:オーディオをBluetoothデバイスに変換して戻す

for input in AVAudioSession.sharedInstance().availableInputs!{ 
      if input.portType == AVAudioSessionPortBluetoothA2DP || input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothLE{ 
       let bluetooth = UIAlertAction(title: input.portName, style: .default, handler: { 
        (alert: UIAlertAction!) -> Void in 
        let audioSession = AVAudioSession.sharedInstance() 
        do { 
         try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) 
         try audioSession.setActive(true) 
        } catch { 
         fatalError("Error Setting Up bluetooth output \(input.portName)") 
        } 


       }) 

       bluetooth.setValue(UIImage(named:"bluetooth.png"), forKey: "image") 
       optionMenu.addAction(bluetooth) 
      } 

let iphomeOutput = UIAlertAction(title: "iPhone", style: .default, handler: { 
       (alert: UIAlertAction!) -> Void in 
       let audioSession = AVAudioSession.sharedInstance() 
       do { 
        do { 
         try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.duckOthers) 
         try audioSession.setActive(true) 
        } catch { 
         fatalError("Error Setting Up audio output Phone") 
        } 
        try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.none) 
       } catch let error as NSError { 
        print("audioSession error turning off speaker: \(error.localizedDescription)") 
       } 
      }) 

      for description in currentRoute.outputs { 
       if description.portType == AVAudioSessionPortHeadsetMic{ 
        optionMenu.setValue(true, forKey: "checked") 
        break 
       } 
      } 
      optionMenu.addAction(iphomeOutput) 

答えて

2

表示されないコードです私は使用を終了しました:

var deviceAction = UIAlertAction() 
     var headphonesExist = false 

     let audioSession = AVAudioSession.sharedInstance() 
     let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) 
     let currentRoute = audioSession.currentRoute 
     for input in audioSession.availableInputs!{ 
      if input.portType == AVAudioSessionPortBluetoothA2DP || input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothLE{ 
           let localAction = UIAlertAction(title: input.portName, style: .default, handler: { 
            (alert: UIAlertAction!) -> Void in 

            do { 
             try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.none) 
            } catch let error as NSError { 
             print("audioSession error turning off speaker: \(error.localizedDescription)") 
            } 

            do { 
             try audioSession.setPreferredInput(input) 
            }catch _ { 
             print("cannot set mic ") 
            } 


           }) 

       for description in currentRoute.outputs { 
        if description.portType == AVAudioSessionPortBluetoothA2DP { 
         localAction.setValue(true, forKey: "checked") 
         break 
        }else if description.portType == AVAudioSessionPortBluetoothHFP { 
          localAction.setValue(true, forKey: "checked") 
          break 
        }else if description.portType == AVAudioSessionPortBluetoothLE{ 
         localAction.setValue(true, forKey: "checked") 
         break 
        } 
       } 
       localAction.setValue(UIImage(named:"bluetooth.png"), forKey: "image") 
        optionMenu.addAction(localAction) 

      } else if input.portType == AVAudioSessionPortBuiltInMic || input.portType == AVAudioSessionPortBuiltInReceiver { 

       deviceAction = UIAlertAction(title: "iPhone", style: .default, handler: { 
        (alert: UIAlertAction!) -> Void in 

        do { 
         try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.none) 
        } catch let error as NSError { 
         print("audioSession error turning off speaker: \(error.localizedDescription)") 
        } 

        do { 
         try audioSession.setPreferredInput(input) 
        }catch _ { 
         print("cannot set mic ") 
        } 

       }) 

       for description in currentRoute.outputs { 
        if description.portType == AVAudioSessionPortBuiltInMic || description.portType == AVAudioSessionPortBuiltInReceiver { 
         deviceAction.setValue(true, forKey: "checked") 
         break 
        } 
       } 

      } else if input.portType == AVAudioSessionPortHeadphones || input.portType == AVAudioSessionPortHeadsetMic { 
       headphonesExist = true 
       let localAction = UIAlertAction(title: "Headphones", style: .default, handler: { 
        (alert: UIAlertAction!) -> Void in 

        do { 
         try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.none) 
        } catch let error as NSError { 
         print("audioSession error turning off speaker: \(error.localizedDescription)") 
        } 

        do { 
         try audioSession.setPreferredInput(input) 
        }catch _ { 
         print("cannot set mic ") 
        } 
       }) 
       for description in currentRoute.outputs { 
        if description.portType == AVAudioSessionPortHeadphones { 
         localAction.setValue(true, forKey: "checked") 
         break 
        } else if description.portType == AVAudioSessionPortHeadsetMic { 
         localAction.setValue(true, forKey: "checked") 
         break 
        } 
       } 

       optionMenu.addAction(localAction) 
      } 
     } 

     if !headphonesExist { 
      optionMenu.addAction(deviceAction) 
     } 

     let speakerOutput = UIAlertAction(title: "Speaker", style: .default, handler: { 
      (alert: UIAlertAction!) -> Void in 

      do { 
       try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker) 
      } catch let error as NSError { 
       print("audioSession error turning on speaker: \(error.localizedDescription)") 
      } 
     }) 
     for description in currentRoute.outputs { 
      if description.portType == AVAudioSessionPortBuiltInSpeaker{ 
       speakerOutput.setValue(true, forKey: "checked") 
       break 
      } 
     } 
     speakerOutput.setValue(UIImage(named:"speaker.png"), forKey: "image") 
     optionMenu.addAction(speakerOutput) 

     let cancelAction = UIAlertAction(title: "Hide", style: .cancel, handler: { 
      (alert: UIAlertAction!) -> Void in 

     }) 
     optionMenu.addAction(cancelAction) 
     self.present(optionMenu, animated: true, completion: nil) 
+0

これは素晴らしいです。ありがとう。 –

+0

良い仕事、ありがとう。 – FreeGor

関連する問題