0

我有一个上传到服务器的视频文件。该文档的名称不包含任何扩展名。我必须从服务器下载该文档(实际上是一个视频文件)并播放它。我需要知道如何获得该文件的扩展名。我没有在我的 URL 中指定它。

它就像/var/mobile/Applications/B18D9BE8-6E1D-43F0-8240-A909B9A27F7C/Documents/XXX/docdata/ABC/XYZ

XYZ 是我要播放的文件。它实际上是一个保存在服务器上的视频文件。

在 Android 中,我们执行解析 URI 之类的操作。我们在 iOS 中有类似的东西吗?

4

2 回答 2

0
The following solution worked for me. Hope it help others as well.

        NSString* fullPath = [yourpath stringByExpandingTildeInPath];
        NSURL* fileUrl = [NSURL fileURLWithPath:fullPath];
        NSURLRequest* fileUrlRequest = [[NSURLRequest alloc] initWithURL:fileUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:.1];

        NSError* error = nil;
        NSURLResponse* response = nil;
        NSData* fileData = [NSURLConnection sendSynchronousRequest:fileUrlRequest returningResponse:&response error:&error];

        NSString* mimeType = [response MIMEType];
        NSArray *components = [mimeType componentsSeparatedByString:@"/"];
        NSString *query = [components lastObject]; //gives the extension

此查询是文件扩展名。您可以根据您的扩​​展放置 if/else 或 switch 来执行操作。

        if ([query isEqualToString:@"mp4"]) {
             yourpath = [yourpath stringByAppendingPathExtension:@"mp4"];
        }
        // likewise for all extensions

        NSData* video = [NSData dataWithContentsOfURL:fileUrl options:NSDataReadingUncached error:&error];
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            [error release];
        } else {
            NSLog(@"Data has loaded successfully.");
        }

        BOOL success = [video writeToFile:yourpath atomically:NO];

        [fileUrlRequest release];
        if (success) {
             //open your video the way you want
        }
于 2013-11-12T13:47:44.627 回答
0

一个通用的方法。查看文件的元数据以确定它是哪种视频类型。这应该在文件本身中提供。

也许这有帮助:https ://stackoverflow.com/a/5815629/1933185

于 2013-11-11T10:59:08.797 回答