2016-08-10 8 views
18

मैं आईओएस में एक वीडियो कैप्चर करने के लिए स्विफ्ट के AVFoundation के साथ काम कर रहा हूं। लेकिन जब मैं ऐप्पल म्यूजिक/स्पॉटिफी का उपयोग करके एक गाना बजता हूं तो मेरे ऐप के रिकॉर्ड बटन पर क्लिक करें, यह संगीत को रोक देता है/रोकता है और फिर वीडियो रिकॉर्ड करता है। मैं इसे कैसे होने से रोक सकता हूं?डिवाइस से ध्वनि/संगीत को रोक दिए बिना वीडियो रिकॉर्ड करना

@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() 
     } 



    } 

सूचना है कि मैं बाहर captureSession.addInput(audioInput) टिप्पणी की:

यहाँ मेरी कोड है। अगर मैं उस कोड को हटा देता हूं, तो ऐप वीडियो रिकॉर्ड करने में सक्षम होता है, यह संगीत को रोक नहीं/रोकता है लेकिन वीडियो आउटपुट में कोई आवाज नहीं है। क्या इसके आसपास कोई कार्य है?

उत्तर

2

मैं इस मुद्दे को हल करने में कामयाब रहा। रेखा: AVAudioSessionCategoryOptions.MixWithOthers कुछ भी नहीं करता है। मैंने इसे विकल्पों में ले जाया: try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers])

यह काम किया!

2

आप इस Question

मैं SCRecorder लाइब्रेरी का उपयोग एक ही सुविधा को लागू किया है की जवाब देने के लिए उल्लेख कर सकते हैं, लेकिन साथ ही AVCaptureSession साथ प्राप्त किया जा सकता है।

+0

मुझे ऑडियो सत्र मोड सेट नहीं किया जा रहा है: त्रुटि डोमेन = NSOSStatusErrorDomain कोड = -50 "(शून्य)" और 'इसे जोड़ने के बाद' वीडियो इनपुट नहीं जोड़ सकता ':' self.captureSession.automaticallyConfiguresAplicationAudioSession = false ; ' –

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