2016-04-29 26 views
6

Sto utilizzando uno WKWebView per visualizzare un video YouTube a schermo intero nella mia app e un AVCaptureSession per registrare audio e video in background durante la navigazione e la riproduzione di video su YouTube. La sessione di acquisizione inizia quando viene premuto un pulsante. Tuttavia, mentre è in corso una registrazione, quando viene selezionato un video di YouTube e inizia la riproduzione a schermo intero, termina immediatamente la registrazione in modo imprevisto quando viene chiamato il metodo delegato che gestisce la fine di una registrazione.La riproduzione di video in visualizzazione Web interrompe inaspettatamente la registrazione del video in background?

Per favore qualcuno potrebbe spiegarmi come risolvere questo problema? Non sono sicuro se questo è completamente correlato, ma ho ricevuto messaggi di errore come _BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15) and Unable to simultaneously satisfy constraints., anche se quest'ultimo sembra fare riferimento a un problema di AutoLayout separato. Qualsiasi aiuto sarebbe molto apprezzato.

Inoltre, ho provato a utilizzare UIWebView anziché WKWebView. Quando utilizzo lo UIWebView, il problema è che il video di YouTube non viene riprodotto nemmeno durante la registrazione del video. Si aprirà e rimarrà alle 0:00 su uno schermo nero.

Questo è il pulsante premuto per avviare inizialmente la registrazione.

- (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]; 
    } 
} 

Questo è il metodo chiamato quando si preme il pulsante. Imposta e avvia la sessione di cattura indicata come CaptureSession.

- (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]; 
} 

Qui è dove viene dichiarato e impostato WKWebView.

- (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

hai provato AVAudioSessionCategoryPlayAndRecord. – mkto

+0

Prova questo http://codethink.no-ip.org/wordpress/archives/673 – Imran

risposta

4

Sembra che le uniche cose che mancano è l'impostazione del mix altri opzione e gioco e registrare con categoria nella sessione audio.

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

Modifica: consente di impostare un test semplice e funziona, sperare che questo aiuti!

+0

Grazie mille per questo. Questo ha aiutato a correggere numerosi bug nell'app. Lo apprezzo molto. –

+0

Ottimo per vedere risolto il problema di Mateo! Ti assegnerò la taglia di +300 in 14 ore quando è dovuta. – MCKapur

+0

@MateoEncarnacion Il mio piacere! Felice di poter essere d'aiuto. – hola

1

La causa del

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

può essere dovuto al fatto che Apple ha introdotto App Transport Security, che impone l'uso di 'HTTPS' URL. Sebbene tu possa modificare il tuo info.plist per fare un'eccezione, per gli URLs legacy, ti consigliamo vivamente di utilizzare HTTPS per qualsiasi nuovo URL.

Nel tuo caso, che è una soluzione semplice se si aggiorna l'URL di YouTube per 'https:'

Problemi correlati