我有一个如下所示的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存储在Core Data数据库中以供重复使用)。
所以,我的问题是:
a)考虑到我可以使用类似于上面的URL运行的所有测试,我假设它可以使用任何这样的URL,这是不是错了?
和
谢谢,提前感谢您能提供的任何帮助。
发布于 2013-09-10 15:44:05
好了,这已经解决了,如果它能帮助其他人,这里就开始了。
为了测试视频在播放时是否仍在设备上,使用了以下代码:
- (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];
}];
}它获取资源url并将其与库进行比较;如果视频可用,if(! asset )将返回true并显示警告视图(代理将其交给视图控制器完成,但它可以在此处执行所需的任何操作。失败块捕获用户拒绝访问设备上的照片的情况。
正如在原始问题中所提到的,上面给出的html可以很好地显示视频。因此,我首先检查URL是否存在,如果存在,则继续播放它。无论视频是youtube还是存储在设备上的视频,都使用相同的URL子类;基本上只有UIWebView和videoID不同(videoID包含youtube的youtube id和本地的资源URL。
感谢您对此的回复。
https://stackoverflow.com/questions/18707207
复制相似问题