2013年8月12日 星期一

不存在則下載後reload,已經存在則直接reload

//下載影片後reload
- (void)downloadVideoAndReload{
    NSString *urlAsString = @"http://yourURL/yourVideo.mp4";
    NSURL *url = [NSURL URLWithString:urlAsString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
   
    //取得文件目錄
    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    //完整檔案路徑
    NSString* filePath = [documentsDir stringByAppendingString:@"marq-playboy-360p.mp4"];
    EAGLView* arView = [arParentViewController getARView];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        NSLog(@"yourVideo.mp4不存在,開始下載");
        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *responce, NSData *data, NSError *error) {
            if ([data length] > 0 && error == nil) {
               
                //將資料寫入檔案
                [data writeToFile:filePath atomically:YES];
               
                NSLog(@"成功存檔至%@",filePath);
                                    // For each video-augmented target
                    for (int i = 0; i < NUM_VIDEO_TARGETS; ++i) {
                        // Load a local file for playback and resume playback if video was
                        // playing when the app went into the background
                        VideoPlayerHelper* player = [arView getVideoPlayerHelper:i];
                        NSString* filename;

                        switch (i) {
                            case 0:
                                //filename = @"/var/mobile/Applications/56AA8666-2109-49C5-A649-F7600240CF15/Library/Documentationht-inception-360p.mp4";
                                filename = filePath;
                                break;
                            default:
                                filename = @"yourVideo.mp4";
                                break;
                        }
                       
                        if (NO == [player load:filename playImmediately:NO fromPosition:videoPlaybackTime[i]]) {
                            NSLog(@"Failed to load media");
                        }
                    }


            }
            else if ([data length] == 0 && error == nil){
                NSLog(@"沒有被下載");
            }
            else if (error != nil){
                NSLog(@"Err : %@",error);
            }
        }];
       
       
    }else{
        NSLog(@"yourVideo.mp4存在");

        // For each video-augmented target
        for (int i = 0; i < NUM_VIDEO_TARGETS; ++i) {
            // Load a local file for playback and resume playback if video was
            // playing when the app went into the background
            VideoPlayerHelper* player = [arView getVideoPlayerHelper:i];
            NSString* filename;
           
            switch (i) {
                case 0:
                    filename = filePath;
                    break;
                default:
                    filename = @"yourVideo.mp4";
                    break;
            }
           
            if (NO == [player load:filename playImmediately:NO fromPosition:videoPlaybackTime[i]]) {
                NSLog(@"Failed to load media");
            }
        }


    }
}

沒有留言:

張貼留言