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

问题描述 投票:0回答:1

在我的应用中,显示照片中的所有视频。选择视频后,将使用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“不允许操作”}}

编辑:我已将url更改为path。但仍显示错误:

   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

文件大小错误:错误Domain = NSCocoaErrorDomain Code = 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

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

ios iphone swift nserror
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.