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 は確実に使用できないと感じています。ただし、上記のようにムービーを問題なく一貫して再生できました。

だから、私の質問は次のとおりです。

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 に答える