【问题标题】:Playing video in web view unexpectedly ends background video recording?在 Web 视图中播放视频意外结束后台视频录制?
【发布时间】:2016-04-29 00:20:34
【问题描述】:

我使用WKWebView 在我的应用中显示全屏 YouTube 视频,并使用 AVCaptureSession 在后台录制音频和视频,同时在 YouTube 上浏览和播放视频。当按下按钮时,捕获会话开始。但是,在录制过程中,当一个 YouTube 视频被选中并开始全屏播放时,它会立即意外结束录制,因为调用了处理录制结束的委托方法。

请有人向我解释如何解决这个问题?不太确定这是否完全相关,但我收到了诸如_BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15) and Unable to simultaneously satisfy constraints. 之类的错误消息,尽管后者似乎引用了一个单独的 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];
}

【问题讨论】:

标签: ios objective-c iphone avfoundation wkwebview


【解决方案1】:

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

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

编辑:设置一个简单的测试,它可以工作,希望这会有所帮助!

【讨论】:

  • 非常感谢您。这也有助于修复应用程序中的许多错误。我真的很感激。
  • 很高兴看到 Mateo 的问题得到解决!到期后,我会在 14 小时内奖励你 +300 赏金。
  • @MateoEncarnacion 我的荣幸!很高兴我能帮上忙。
【解决方案2】:

原因

_BSMachError: (os/kern) 无效能力 (20) _BSMachError: (os/kern) 无效名称 (15)

可能是因为 Apple 引入了强制使用“HTTPS”URL 的 App Transport Security。尽管您可以修改 info.plist 以破例,但对于旧 URL,他们强烈建议对任何新 URL 使用 HTTPS。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2012-08-09
    • 2015-10-06
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多