在Web视图中播放视频意外终止了后台视频录制?

6
我正在使用WKWebView在我的应用中显示全屏YouTube视频,并使用AVCaptureSession在浏览和播放YouTube视频时在后台记录音频和视频。当按下按钮时,捕获会话开始。然而,在录制过程中,当选择并开始播放全屏YouTube视频时,它会立即意外地结束录制,因为调用了处理录制结束的委托方法。
请问有人能解释一下如何解决这个问题吗?我不太确定这是否完全相关,但我收到了错误消息,例如_BSMachError:(os / kern)无效能力(20)_BSMachError:(os / kern)无效名称(15)和无法同时满足约束。,虽然后者似乎是指一个单独的AutoLayout问题。任何帮助都将不胜感激。
此外,我已经尝试使用UIWebView代替WKWebView。当我使用UIWebView时,问题在于当视频正在录制时,YouTube视频甚至无法播放。它只会打开并停留在黑屏的0:00。
以下是初始启动录制的按钮。
- (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];
    }
}

当按钮被按下时,将调用此方法。它会设置并启动被称为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];
}

这是声明并设置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];
}

你试过 AVAudioSessionCategoryPlayAndRecord 吗? - mkto
请尝试访问 http://codethink.no-ip.org/wordpress/archives/673 - Imran
2个回答

6

看起来唯一缺少的是在音频会话中设置与其他人混合选项和播放和录制类别。

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

编辑:我已经设置了一个简单的测试,它可以正常工作,希望这对你有所帮助!

非常感谢您的帮助。这有助于修复应用程序中的许多错误。我非常感激。 - Mateo Encarnacion
很高兴看到Mateo的问题得到解决!当奖励到期时,我将在14小时内授予您+300赏金。 - MCKapur
@MateoEncarnacion 不客气!很高兴我能帮到你。 - hola

1

引起_BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15)的原因可能是苹果引入了应用程序传输安全性,这强制使用“HTTPS” URL。尽管您可以修改info.plist以便为旧的URLS做出例外,但他们强烈建议对于任何新的URLS都使用HTTPS。

如果您只是将您的YouTube URL更新为“https:”,那么这个问题很容易解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接