我一直在为iOS 8编写一个相机应用程序,它使用AVFoundation来设置和处理录音和保存(不是 ImagePickerController).我试图保存使用AVCaptureMovieFileOutput类的maxRecordedFileSize属性,以允许用户填充手机上的所有可用空间(减去苹果的剩余250MB缓冲区).
- (unsigned long long) availableFreespaceInMb {
unsigned long long freeSpace;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

if (dictionary) {
    NSNumber *fileSystemFreeSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize];
    freeSpace = [fileSystemFreeSizeInBytes unsignedLongLongValue];

} else {
    NSLog(@"Error getting free space");
    //Handle error
}

//convert to MB
freeSpace = (freeSpace/1024ll)/1024ll;
freeSpace -= _recordspaceBufferInMb; // 250 MB
NSLog(@"Remaining space in MB: %llu",freeSpace);
NSLog(@"    Diff Since Last: %llu",(_prevRemSpaceMb - freeSpace));

_prevRemSpaceMb = freeSpace;
return freeSpace;

}

当可用空间(减号)减少为零时,抛出AVErrorMaximumFileSizeReached,并且不会抛出保存错误,但视频不会出现在相机胶卷中,并且未保存.当我设置maxRecordedDuration字段时,抛出AVErrorMaximumDurationReached,并保存视频.我从最大大小计算最大时间,但由于帧压缩,我总是留有足够的空间.

- (void) toggleMovieRecording
{
double factor = 1.0;
if (_currentFramerate == _slowFPS) {
    factor = _slowMotionFactor;
}

double availableRecordTimeInSeconds = [self remainingRecordTimeInSeconds] / factor;
unsigned long long remainingSpace = [self availableFreespaceInMb] * 1024 * 1024;

if (![[self movieFileOutput] isRecording]) {
    if (availableSpaceInMb < 50) {
        NSLog(@"TMR:Not enough space,can't record");
        [AVViewController currentVideoOrientation];
        [_previewView memoryAlert];
        return;
    }
}

if (![self enableRecording]) {
    return;
}

[[self recordButton] setEnabled:NO];

dispatch_async([self sessionQueue],^{
    if (![[self movieFileOutput] isRecording])
    {            
        if ([[UIDevice currentDevice] isMultitaskingSupported])
        {
            [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]];
        }

        // Update the orientation on the movie file output video connection before starting recording.
        [[[self movieFileOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation: [AVViewController currentVideoOrientation]];//[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

        // Start recording to a temporary file.
        Nsstring *outputFilePath = [NstemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]];

        // Is there already a file like this?
        NSFileManager *fileManager = [NSFileManager defaultManager];

        if ([fileManager fileExistsAtPath:outputFilePath]) {
            NSLog(@"filexists");
            NSError *err;
            if ([fileManager removeItemAtPath:outputFilePath error:&err] == NO) {
                NSLog(@"Error,file exists at path");
            }
        } 

        [_previewView startRecording];

        // Set the movie file output to stop recording a bit before the phone is full
        [_movieFileOutput setMaxRecordedFileSize:remainingSpace]; // Less than the total remaining space
       // [_movieFileOutput setMaxRecordedDuration:CMTimeMake(availableRecordTimeInSeconds,1.0)];

        [_movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self];
    }
    else
    {
        [_previewView stopRecording];
        [[self movieFileOutput] stopRecording];
    }
});
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error

NSLog(@"AVViewController: didFinishRecordingToOutputFile");

if (error) {
    NSLog(@"%@",error);
    NSLog(@"Caught Error");
    if ([error code] == AVErrordiskFull) {
        NSLog(@"Caught disk full error");
    } else if ([error code] == AVErrorMaximumFileSizeReached) {
        NSLog(@"Caught max file size error");
    } else if ([error code] == AVErrorMaximumDurationReached) {
        NSLog(@"Caught max duration error");
    } else {
        NSLog(@"Caught other error");
    }

    [self remainingRecordTimeInSeconds];

    dispatch_async(dispatch_get_main_queue(),^{
        [_previewView stopRecording];
        [_previewView memoryAlert];
    });
}

// Note the backgroundRecordingID for use in the ALAssetsLibrary completion handler to end the background task associated with this recording. This allows a new recording to be started,associated with a new uibackgroundtaskIdentifier,once the movie file output's -isRecording is back to NO — which happens sometime after this method returns.
uibackgroundtaskIdentifier backgroundRecordingID = [self backgroundRecordingID];
[self setBackgroundRecordingID:uibackgroundtaskInvalid];

[[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL,NSError *error) {
    if (error) {
        NSLog(@"%@",error);
        NSLog(@"Error during write");
    } else {
        NSLog(@"Writing to photos album");
    }

    [[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil];

    if (backgroundRecordingID != uibackgroundtaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask:backgroundRecordingID];
}];

if (error) {
    [_session stopRunning];
    dispatch_after(dispatch_time(disPATCH_TIME_Now,1.0 * NSEC_PER_SEC),_sessionQueue,^{
        [_session startRunning];
    });
}

当两个错误被抛出时,会出现“写入照片相册”.我完全被这个困扰了任何iOS的见解?

解决方法

您提供的代码示例很难测试,因为缺少一个属性和方法.虽然我无法编译你的代码,但肯定会有一些红旗可能导致这个问题.以下问题发现在:
captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:错误:

问题1:该方法正在处理传递的错误,但是继续执行该方法.而应该是:

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error { 
    if (error) {
        // handle error then bail
        return;
    }

    // continue on
}

问题2:您要实例化的ALAssetsLibrary对象不会存储到一个属性中,因此一旦captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:finish对象将被释放(可能永远不会触发完成块).相反应该是:

// hold onto the assets library beyond this scope
self.AssetsLibrary = [[ALAssetsLibrary alloc] init];

// get weak reference to self for later removal of the assets library
__weak typeof(self) weakSelf = self;
[self.AssetsLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL,NSError *error) {
    // handle error
    // handle cleanup

    // cleanup new property
    weakSelf.AssetsLibrary = nil;
}];

如果修复这些问题无法解决问题,请提供缺少的代码,使您的示例编译.

ios – 使用maxRecordedFileSize限制AVCaptureSession记录时间的更多相关文章

  1. ios – UIScrollView内容不允许用户交互

    我有一个启用了分页的UIScrollView,如下所示:在UIScrollView中,我添加了几个UIWebViews,并将其启用的交互设置为是这样的.它打破了UIScrollView上的分页和所有触摸.如果我将用户交互设置为NO,则页面有效,但我无法在UIWebView中突出显示文本.我试着像下面那样对UIScrollView进行子类化,但是会出现同样的情况.任何的想法?

  2. iOS上的GCDAsyncUdpSocket缺少多播数据报

    我还看到GCDAsyncUdpSocket中有一个readStream方法.也许我需要使用它而不是beginReceiving?

  3. ios – UINavigationBar -pushNavigationItem在将新控制器推送到UINavigationController堆栈时从不调用

    解决方法我找到了原因:它调用–pushNavigationItem:项不调用–pushNavigationItem:animated!

  4. ios – Touch拖动输入如何工作?

    任何人都可以给我一个关于触摸拖动输入以从一个按钮拖动到另一个触发两个按钮事件的示例.它是如何工作的?例如,我想从Do到Fa拖动Do,Re,Mi,Fa的事件被触发.这是我的代码:哦,我已经看到,当我按住Simulator时,会触发方法btnDragOut.当我从Simulator拖出按钮时,会触发此按钮的方法.现在我希望当我拖出一个按钮时触发方法btnDragOut.有谁知道吗?

  5. ios – avplayer没有播放网址

    我正在使用avplayer播放音频网址,但它无法正常工作,我不知道我错在哪里任何帮助,将不胜感激.解决方法我强烈推荐下面的代码来播放无线电流:请看一下AVPlayer_Class参考链接是–Streamingmp3audiowithAVPlayer

  6. 从AVFullScreenViewController iOS 8获取媒体链接

    我想在某些内容开始播放时尝试获取视频链接.首先我抓住视频开始播放然后延迟尝试获取链接CUViewController,CUPlayerView,CUPlayerController–假的类,它看起来像这样一切都还行,直到这一行球员总是零.也许有更简单的方式来获得媒体链接?

  7. ios – 未提示在应用程序中启用位置服务

    更新:这不是重复.我已经在info.plist中添加了所需的密钥,如我原始问题中所述,问题仍然存在.我已经尝试了各种组合的所有三个键.在任何人感到不安之前,我已阅读了许多AppleDev论坛帖子和堆栈溢出帖子,无法弄清楚为什么我的应用程序拒绝提示用户允许使用时授权.我已将以下密钥添加到我的Info.plist文件中,并附带一个String值:然后我写了(在Swift和Obj-C中)应该提示用户的代

  8. ios – NSLocalizedString有时会加载字符串,而不是总是

    解决方法如果您的localisable文件中有任何额外的分号,它会构建但无法进行本地化.

  9. ios – NSOperation Queue行为异常

    .2)完成块采用一系列操作…您可以查看数组长度以查看.如果确实调用了操作子集,那么这将是删除已经上载的资产并执行清理工作的地方,因为您知道哪些操作已完成.3)我会在将操作添加到队列之前设置上传进度块;只是为了安全.4)我找不到batchOperation方法的文档,并想知道它是否已从更新版本的AFNetworking中删除–如果是这样–也许它是错误的或不好的API?它在SIM上与在单元网络上的行为方式相同还是模拟的慢/不可靠连接?

  10. iOS:使用CFStreamCreatePairWithSocketToHost的套接字网络基础

    >每次要发送新数据对象时,是否设置了新套接字?>我是否必须重置outputStream并发送更多数据.码大部分代码来自CocoaStreamsDocumentation:响应:请注意,在发送数据后,outputStream流将关闭.我尝试在[selfsendString:@“AnotherTest”]之前重新启动outputStream.我也试过了idz的回答.根据文档,我相信len:0的发送缓冲区是我的问题.IfthedelegatereceivesanNsstreamEventHasspaceAvai

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部