Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recording a Video without stopping sound/music from device

I am working with Swift's AVFoundation to capture a video in iOS. But when I play a song using Apple Music/Spotify then click the record button of my app, it pauses/stops the music then records the video. How do I prevent that from happening?

Here's my code:

@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(AVAudioSessionCategoryAmbient)
            }
            catch
            {
                print("Can't Set Audio Session Category: \(error)")
            }
            AVAudioSessionCategoryOptions.MixWithOthers

            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)
            do
            {
                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)")
            }
            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()
        }



    }

Notice that I commented out captureSession.addInput(audioInput). If I remove that code, the app is able to record the video, it doesn't pause/stop the music but the video output has no sound. Is there a workaround for this?

like image 293
Jayson Tamayo Avatar asked Aug 10 '16 13:08

Jayson Tamayo


2 Answers

I managed to solve the issue on my own. The line: AVAudioSessionCategoryOptions.MixWithOthers doesn't do anything. I moved it to the options: try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers])

It worked!

like image 180
Jayson Tamayo Avatar answered Nov 05 '22 18:11

Jayson Tamayo


You can refer to the answer of this Question

I have implemented same feature using SCRecorder Library, but can be achieve with AVCaptureSession as well.

like image 2
Surjeet Singh Avatar answered Nov 05 '22 19:11

Surjeet Singh