尝试获取视频文件的大小(kb)时出错

时间:2018-09-03 16:54:46

标签: ios iphone swift nserror

在我的应用中,显示照片中的所有视频。选择视频后,将使用avplayer播放。但是,当我尝试获取所选视频文件的大小(kb)时,它显示错误。当我尝试复制视频文件时,出现了相同的错误。

我已经获得了这些权限:

<key>NSCameraUsageDescription</key>
<string>App needs to access camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>App need to access microphone</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App needs to access photos and videos</string>

获得尺寸的代码:

func fileSize(forURL url: Any) -> Double {

    var fileURL: URL?
    var fileSize: Double = 0.0

    if url is URL || url is String {

        if url is URL {
            fileURL = url as? URL
        }
        else {
            fileURL = URL(fileURLWithPath: url as! String)
        }
        var fileSizeValue = 0.0

        do{
            try fileSizeValue = (fileURL?.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! Double?)!
            if fileSizeValue > 0.0 {
                fileSize = (Double(fileSizeValue) / (1024 * 1024))
            }

        } catch {

            print("file size error: \(error)")
        }
    }
    return fileSize
}

let fileSize = fileSize(forURL: selectedVideos[0] as Any)
  

视频文件网址:“ file:///var/mobile/Media/DCIM/107APPLE/IMG_7728.MOV”

     

错误:错误域= NSCocoaErrorDomain代码= 257“由于您没有权限打开文件“ IMG_7728.MOV”,因此无法打开   查看。”   UserInfo = {NSURL = file:///var/mobile/Media/DCIM/107APPLE/IMG_7728.MOV,   NSFilePath = / var / mobile / Media / DCIM / 107APPLE / IMG_7728.MOV,   NSUnderlyingError = 0x1c445ef90 {Error Domain = NSPOSIXErrorDomain Code = 1   “不允许操作”}}

编辑:  我已将网址更改为路径。但仍然显示错误:

   let selectedFilePath = (selectedVideos[0] as! URL).path
    print("video path: \(selectedFilePath)")
    do{
        let attributes = try FileManager.default.attributesOfItem(atPath: selectedFilePath)
        let fileSize = attributes[.size] as! NSNumber
        print("file size: \(fileSize)")
    } catch {  
        print("file size error: \(error)")
    }

    let filePath = get_directory().appendingPathComponent("video\(CACurrentMediaTime()).MOV")
    print("copy to path: \(filePath.path)")
    do {
        try FileManager.default.copyItem(atPath: selectedFilePath, toPath: filePath.path)

    } catch {

        print("copy error: \(error)")
    }

错误:

  

视频路径:/var/mobile/Media/DCIM/107APPLE/IMG_7789.MOV

     

文件大小错误:Error Domain = NSCocoaErrorDomain代码= 257“由于您无权查看文件“ IMG_7789.MOV”,因此无法打开该文件。” UserInfo = {NSFilePath = / var / mobile / Media / DCIM / 107APPLE / IMG_7789.MOV,NSUnderlyingError = 0x1c0259530 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}

     

复制到路径:/var/mobile/Containers/Data/Application/5AAD29BD-489D-4737-AA98-E6131EFBA94D/Documents/addMusicToVideos/voices/video1171047.69338271.MOV

     

复制错误:错误域= NSCocoaErrorDomain代码= 257“由于您无权查看文件“ IMG_7789.MOV”,因此无法打开该文件。” UserInfo = {NSFilePath = / var / mobile / Media / DCIM / 107APPLE / IMG_7789.MOV,NSUnderlyingError = 0x1c4859830 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}

1 个答案:

答案 0 :(得分:0)

使用此 AVURLAsset 扩展名,您可以非常容易地从URL获取视频文件的大小。确保导入 AVFoundation 框架。代码如下:

extension AVURLAsset {
    var fileSize: Int? {
    let keys: Set<URLResourceKey> = [.totalFileSizeKey, .fileSizeKey]
    let resourceValues = try? url.resourceValues(forKeys: keys)

    return resourceValues?.fileSize ?? resourceValues?.totalFileSize
    }
} 

此扩展名以 Int (字节)为单位返回视频大小。您可以根据需要修改单位。要调用它,请使用以下方法:

let asset = AVURLAsset(url: urlOfYourAsset)
print(asset.fileSize ?? 0)

您没有提供足够的信息。问题出在您的文件系统上。将视频文件放在iOS中的位置以及如何获取URL都很重要,这就是问题所在。可能是沙箱中不允许使用的URL。请进一步研究并阅读Apple Documentation