2016-04-29 12 views
6

Ich verwende eine WKWebView, um ein Fullscreen-YouTube-Video in meiner App anzuzeigen, und eine AVCaptureSession, um Audio und Video im Hintergrund aufzuzeichnen, während ich Videos auf YouTube durchsuche und wiedergebe. Die Aufnahmesitzung beginnt, wenn eine Schaltfläche gedrückt wird. Während der Aufnahme wird jedoch ein YouTube-Video ausgewählt und die Wiedergabe im Vollbildmodus gestartet. Die Aufzeichnung wird dann unerwartet beendet, wenn die Delegate-Methode zum Beenden einer Aufzeichnung aufgerufen wird.Die Videoaufnahme in der Webansicht beendet die Videoaufnahme im Hintergrund unerwartet?

Bitte könnte mir jemand erklären, wie ich dieses Problem lösen kann? Nicht sicher, ob das vollständig verwandt ist, aber ich habe Fehlermeldungen wie _BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15) and Unable to simultaneously satisfy constraints. erhalten, obwohl letzteres auf ein separates AutoLayout-Problem verweist. Jede Hilfe würde sehr geschätzt werden.

Auch habe ich versucht mit UIWebView anstelle von WKWebView. Wenn ich UIWebView verwende, ist das Problem, dass das YouTube-Video nicht einmal abgespielt wird, wenn das Video aufnimmt. Es öffnet sich und bleibt um 0:00 Uhr auf einem schwarzen Bildschirm.

Hier wird die Taste gedrückt, um die Aufnahme zu starten.

- (void) buttonClickedStart:(UIButton*)sender //button to start/end recording { 
    if (!WeAreRecording) { 
     [self setupVideoCapture]; 
     //----- START RECORDING ----- 
     WeAreRecording = YES; 
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; 

     //Create temporary URL to record the video to for later viewing 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *basePath = [paths objectAtIndex:0]; 
     NSString *outputPath = [[NSString alloc] initWithFormat:@"%@", [basePath stringByAppendingPathComponent:@"output.mp4"]]; 
     if ([[NSFileManager defaultManager] isDeletableFileAtPath:outputPath]) 
      [[NSFileManager defaultManager] removeItemAtPath:outputPath error:NULL]; 
     NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath]; 
     [MovieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 
     self.outputURLs = outputURL; 
    } 
    else { 
     //----- STOP RECORDING ----- 
     WeAreRecording = NO; 
     [MovieFileOutput stopRecording]; 
    } 
} 

Diese Methode wird aufgerufen, wenn die Taste gedrückt wird. Es erstellt und startet die Erfassungssitzung, die als CaptureSession bezeichnet wird.

- (void)setupVideoCapture { 
    // Sets up recording capture session 
    CaptureSession = [[AVCaptureSession alloc] init]; 

    // Add video input to capture session 
    AVCaptureDevice *VideoDevice = [AVCaptureDevice  defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    if (VideoDevice) { 
     NSError *error; 
     VideoInputDevice = [[AVCaptureDeviceInput alloc] initWithDevice:[self CameraWithPosition:AVCaptureDevicePositionFront] error:&error]; 
     if (!error) { 
      if ([CaptureSession canAddInput:VideoInputDevice]) 
       [CaptureSession addInput:VideoInputDevice]; 
     } 
    } 

    // Add audio input to capture session 
    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 
    NSError *error = nil; 
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error]; 
    if (audioInput) { 
     [CaptureSession addInput:audioInput]; 
    } 

    // Add movie file output to the capture session 
    MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; 
    [MovieFileOutput setMovieFragmentInterval:kCMTimeInvalid]; 
    if ([CaptureSession canAddOutput:MovieFileOutput]) 
     [CaptureSession addOutput:MovieFileOutput]; 

    // Set the output properties (you don't really need to see this code) 
    [self CameraSetOutputProperties]; // (We call a method as it also has to be done after changing camera) 
    [CaptureSession setSessionPreset:AVCaptureSessionPresetMedium]; 

    //----- START THE CAPTURE SESSION RUNNING ----- 
    [CaptureSession startRunning]; 
} 

Hier wird die WKWebView deklariert und eingerichtet.

- (void)viewDidAppear:(BOOL)animated { 
     [super viewDidAppear:animated]; 

     // Add recording button 
     CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
     CGRect rect = CGRectMake(0, 0, screenSize.width, 337); 
     UIButton *start = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [start addTarget:self action:@selector(buttonClickedStart:) forControlEvents:UIControlEventTouchUpInside]; 
     [start setFrame:CGRectMake(30, 338, 35, 35)]; 
     [start setTitle:@"" forState:UIControlStateNormal]; 
     [start setExclusiveTouch:YES]; 
     [start setBackgroundImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal]; 
     [self.view addSubview:start]; 

     // Add web view 
     webView = [[WKWebView alloc] initWithFrame:rect]; 
     [self.view addSubview:webView]; 
     NSString *webSite = @"http://www.youtube.com"; 
     NSURL *url = [NSURL URLWithString:webSite]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
     webView.navigationDelegate = self; 
     webView.UIDelegate = self; 
     [webView loadRequest:request]; // Load up Youtube   
     [self.view addSubview:webView]; 
} 
+0

haben Sie AVAudioSessionCategoryPlayAndRecord versucht. – mkto

+0

Probieren Sie diese http://codethink.no-ip.org/wordpress/archives/673 – Imran

Antwort

4

Sieht aus wie die einzigen Dinge, die die Mischung mit anderen Option und Aufnahme und Wiedergabe fehlen setzen Kategorie in der Audiositzung.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 

Edit: Richten Sie einen einfachen Test und es funktioniert, hoffe das hilft!

+0

Vielen Dank dafür. Dies half, zahlreiche Bugs in der App zu beheben. Ich schätze es sehr. –

+0

Toll zu sehen Mateo's Problem gelöst! Ich werde dir das +300 Kopfgeld in 14 Stunden geben, wenn es fällig ist. – MCKapur

+0

@MateoEncarnacion Mein Vergnügen! Froh, dass ich helfen könnte. – hola

1

Die Ursache des

_BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15)

kann auf die Tatsache zurückzuführen sein, dass Apple App Transportsicherheit eingeführt, die die Verwendung von URLs ‚HTTPS‘ erzwingt. Sie können zwar Ihre Info.plist ändern, um eine Ausnahme zu machen, für ältere URLs wird jedoch dringend empfohlen, HTTPS für neue URLs zu verwenden.

In Ihrem Fall, dass eine einfache Lösung, wenn Sie nur Ihre auf youtube URL aktualisieren ‚https:‘