jjzjj

ios - 在 WebView 中播放视频意外结束后台视频录制?

coder 2023-09-23 原文

我正在使用 WKWebView 在我的应用程序中显示全屏 YouTube 视频,并使用 AVCaptureSession 在后台录制音频和视频,同时在 YouTube 上浏览和播放视频。按下按钮时捕获 session 开始。 However, whilst a recording is in process, when a YouTube video is selected and starts playing in fullscreen, it immediately ends the recording unexpectedly as the delegate method that handles the ending of a recording is called.

有人可以向我解释如何解决这个问题吗?不太确定这是否完全相关,但我收到了诸如 _BSMachError: (os/kern) invalid capability (20) _BSMachError: (os/kern) invalid name (15) 和 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 的捕获 session 。

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

最佳答案

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

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

编辑:设置一个简单的测试并且它有效,希望这对您有所帮助!

关于ios - 在 WebView 中播放视频意外结束后台视频录制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927545/

有关ios - 在 WebView 中播放视频意外结束后台视频录制?的更多相关文章

  1. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. 屏幕录制为什么没声音?检查这2项,轻松解决 - 2

    相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. 动漫制作技巧如何制作动漫视频 - 2

    动漫制作技巧是很多新人想了解的问题,今天小编就来解答与大家分享一下动漫制作流程,为了帮助有兴趣的同学理解,大多数人会选择动漫培训机构,那么今天小编就带大家来看看动漫制作要掌握哪些技巧?一、动漫作品首先完成草图设计和原型制作。设计草图要有目的、有对象、有步骤、要形象、要简单、符合实际。设计图要一致性,以保证制作的顺利进行。二、原型制作是根据设计图纸和制作材料,可以是手绘也可以是3d软件创建。在此步骤中,要注意的问题是色彩和平面布局。三、动漫制作制作完成后,加工成型。完成不同的表现形式后,就要对设计稿进行加工处理,使加工的难易度降低,并得到一些基本准确的概念,以便于后续的大样、准确的尺寸制定。四、

  7. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  8. TimeSformer:抛弃CNN的Transformer视频理解框架 - 2

    Transformers开始在视频识别领域的“猪突猛进”,各种改进和魔改层出不穷。由此作者将开启VideoTransformer系列的讲解,本篇主要介绍了FBAI团队的TimeSformer,这也是第一篇使用纯Transformer结构在视频识别上的文章。如果觉得有用,就请点赞、收藏、关注!paper:https://arxiv.org/abs/2102.05095code(offical):https://github.com/facebookresearch/TimeSformeraccept:ICML2021author:FacebookAI一、前言Transformers(VIT)在图

  9. ruby - 如何在 ruby​​ 中运行后台线程? - 2

    我是ruby​​的新手,我认为重新构建一个我用C#编写的简单聊天程序是个好主意。我正在使用Ruby2.0.0MRI(Matz的Ruby实现)。问题是我想在服务器运行时为简单的服务器命令提供I/O。这是从示例中获取的服务器。我添加了使用gets()获取输入的命令方法。我希望此方法在后台作为线程运行,但该线程正在阻塞另一个线程。require'socket'#Getsocketsfromstdlibserver=TCPServer.open(2000)#Sockettolistenonport2000defcommandsx=1whilex==1exitProgram=gets.chomp

  10. Ruby - 如何处理子类意外覆盖父类(super class)私有(private)字段的问题? - 2

    假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案

随机推荐