从内置扬声器切换到蓝牙扬声器

时间:2016-08-29 15:12:17

标签: objective-c swift

我刚刚找到一个代码,用于在内置和连接的蓝牙扬声器之间切换扬声器:

changeResult = [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                                               error:&audioError];

但这是在Obj-C中,有没有人在这里将它转换为Swift?

谢谢!

更新:这是我用来录制视频的代码。这是场景。我正在连接蓝牙音箱时播放音乐。因此声音输出在外部扬声器中。单击录制按钮时,声音输出将传输到内置扬声器。这是我的问题。输出应始终位于外部扬声器中。

@IBAction func record_video(sender: AnyObject) {
        var initialOutputURL = NSURL(fileURLWithPath: "")

        do
        {
            initialOutputURL = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent("output").URLByAppendingPathExtension("mov")
        }catch
        {
            print(error)
        }
        if !isRecording
        {

            isRecording = true
            if let outputs = captureSession.outputs as? [AVCaptureOutput] {
                for output in outputs {
                    captureSession.removeOutput(output)
                }
            }

            do
            {
                try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers, AVAudioSessionCategoryOptions.AllowBluetooth])
                //print(audioSession.setOutputDataSource)
            }
            catch
            {
                print("Can't Set Audio Session Category: \(error)")
            }


            do
            {
                try audioSession.setMode(AVAudioSessionModeVideoRecording)
            }
            catch
            {
                print("Can't Set Audio Session Mode: \(error)")
            }
            // Start Session
            do
            {
                try audioSession.setActive(true)
            }
            catch
            {
                print("Can't Start Audio Session: \(error)")
            }


            UIView.animateWithDuration(0.5, delay: 0.0, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations: { () -> Void in
                self.record.transform = CGAffineTransformMakeScale(0.75, 0.75)
                }, completion: nil)

            let audioInputDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
            AVCaptureDevice.devicesWithMediaType(AVMediaTypeAudio)
            do
            {
                if let availableInputs = audioSession.availableInputs {
                    for input in availableInputs {
                        if input.portType == AVAudioSessionPortBuiltInMic ||
                            input.portType == AVAudioSessionPortHeadsetMic {
                            inputs.append(input)
                            try audioSession.setPreferredInput(input)

                        }
                    }
                }

                let audioInput = try AVCaptureDeviceInput(device: audioInputDevice)

                // Add Audio Input
                if captureSession.canAddInput(audioInput)
                {
                    captureSession.addInput(audioInput)
                }
                else
                {
                    NSLog("Can't Add Audio Input")
                }
                /**
                let videoInput: AVCaptureDeviceInput
                do
                {
                    videoInput = try AVCaptureDeviceInput(device: captureDevice)

                    // Add Video Input
                    if captureSession.canAddInput(videoInput)
                    {
                        captureSession.addInput(videoInput)
                    }
                    else
                    {
                        NSLog("ERROR: Can't add video input")
                    }
                }
                catch let error
                {
                    NSLog("ERROR: Getting input device: \(error)")
                }
                */
                try AVAudioSession.sharedInstance().overrideOutputAudioPort(.Speaker)
                videoFileOutput = AVCaptureMovieFileOutput()
                captureSession.addOutput(videoFileOutput)
                captureSession.sessionPreset = AVCaptureSessionPresetHigh
                captureSession.automaticallyConfiguresApplicationAudioSession = false

                videoFileOutput?.startRecordingToOutputFileURL(initialOutputURL, recordingDelegate: self)


            }
            catch let error
            {
                NSLog("Error Getting Input Device: \(error)")
            }

        }
        else
        {
            isRecording = false

            UIView.animateWithDuration(0.5, delay: 0, options: [], animations: { () -> Void in
                self.record.transform = CGAffineTransformMakeScale(1.0, 1.0)
                }, completion: nil)
            record.layer.removeAllAnimations()
            videoFileOutput?.stopRecording()
        }



    }

0 个答案:

没有答案