1

我有一个如下所示的 URL:

assets-library//asset/asset.MOV?id=51CED0CF-223D-4606-81BB-241381BCF2E8&ext=MOV

我正在使用以下代码在 UIWebview 中播放它:

    NSString *localVideoHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-mp4\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
    NSString *html = [NSString stringWithFormat:localVideoHTML, videoId, self.frame.size.width, self.frame.size.height];

视频播放完美;我已经用这些 URL 尝试了几个,都没有问题。但是,我希望能够在播放之前确认它没有从设备中删除。我正在使用以下代码来执行此操作:

NSURL *url = [NSURL URLWithString:videoID];
NSError *err;
BOOL isReachable = [url checkResourceIsReachableAndReturnError:&err];

这似乎适用于某些 URL,但不适用于其他 URL。因此,在这一点上,我很困惑我是否可以事前确定 URL 是否指向现有文件(文件可能已从设备中删除,因为 URL 存储在核心中重复使用的数据库)。我从 SO 和其他地方的阅读中了解到,一个看起来像我发布的 URL 的 URL 不能可靠地使用;但是,我已经能够毫无困难地连续播放如上所示的电影。

所以,我的问题是:

a) 鉴于我能够使用类似于上面的 URL 运行我运行的所有测试,我假设它会使用任何这样的 URL 是否是错误的?

b)在加载到 webview 之前,是否有一种简单的方法来测试 URL 的“可达性”,所以如果它不是有效的 URL,我可以在我的代码中处理这个问题?

谢谢,提前为您提供任何帮助。

4

1 回答 1

0

Okay, this is resolved, and in case it will help someone else, here goes.

To test whether the video is still on the device at the time it is to be played, the following code was used:

- (void) verifyURLReachable:(NSString *) videoID
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:[NSURL URLWithString:videoID]
     resultBlock:^(ALAsset *asset)
     {
         if (!asset)
         {
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video is Not Available on Device"
                                                             message:@"The requested video is no longer available on this iPad device. This video will be automatically removed from the current goal."
                                                            delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
             alert.tag = alertViewURLUnreachable;
             [alert show];
         }
    }
    failureBlock:^(NSError *error)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access to Photos is Required"
                                                        message:@"To play videos that are stored on your device you must allow access to your photo and video library. This setting is made in your device setttings under 'Privacy'."
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        alert.tag = alertViewUserDeniedAccess;
        [alert show];
    }];
}

It takes the asset url and checks it against the library; if the video is available, the if(!asset) returns true and puts up the alert view (the delegate hands it back to the view controller to finish up, but it could do whatever it needed in here. The failure block traps the situation where the user has denied access to photos on the device.

As mentioned in the original question, the html given above works fine for displaying the video. So, I first check that the URL exists and if i does, I proceed to playing it. The same UIWebView subclass is used whether the video is a youtube or a video stored on the device; basically only the HTML and the videoID is different (videoID contains the youtube id for youtubes, and the asset URL for local.

Thanks for your reply(s) on this.

于 2013-09-10T07:44:04.857 回答