2013年6月10日 星期一

下載影片

// 大影片(35 MB)
#define LARGE_URL @"http://www.archive.org/download/BettyBoopCartoons/Betty_Boop_More_Pep_1936_512kb.mp4"

// 小影片(3 MB)
#define SMALL_URL @"http://www.archive.org/download/Drive-inSaveFreeTv/Drive-in--SaveFreeTv_512kb.mp4"

// 無效的影片網址
#define FAKE_URL @"http://www.idontbelievethisisavalidurlforthisexample.com"

#define DEST_PATH    [NSHomeDirectory() stringByAppendingString:@"/Documents/Movie.mp4"]


// 選擇下載哪個項目
    NSArray *items = [NSArray arrayWithObjects: SMALL_URL, LARGE_URL, FAKE_URL, nil];
    NSString *whichItem = [items objectAtIndex:seg.selectedSegmentIndex];
    NSURL *sourceURL = [NSURL URLWithString:whichItem];

    // 移除任何已存在的資料
    if ([[NSFileManager defaultManager] fileExistsAtPath:DEST_PATH])
        [[NSFileManager defaultManager] removeItemAtPath:DEST_PATH error:nil];
   
    // 啟動網路活動指示器
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   
    // 在新的NSOperationQueue裡進行下載
    [[[NSOperationQueue alloc] init] addOperationWithBlock:
     ^{
         [self getData:sourceURL];
         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
             // 在主緒程裡處理收尾工作
             [self downloadFinished];
         }];
     }];

沒有留言:

張貼留言