7

在我的新应用程序中,我需要从不同的网站下载视频,假设它是视频下载器应用程序。为此,我计划在 html 中搜索 .mp4 和 .Flv url,然后尝试下载视频。有很多应用程序已经在做同样的事情

http://itunes.apple.com/in/app/video-downloader-super-lite/id481701140?mt=8

我要问的是,我们如何下载视频?任何代码或链接或其他东西。这个应用程序是如何工作的?任何帮助都会非常感激。

我需要的是当您在 UIWebview 中打开页面时假设您打开“www.youtube.com”并选择要播放的视频然后它要求下载。对于下载,我需要 URL(嵌入式 url、Flv url、mpv url),这样我就可以让它发挥作用。我需要知道那个 URL

4

2 回答 2

1

如果你真的想去黑客,你会得到苹果的私有库“webkit”,如果你尝试找到 UIWebview 的子视图,它可能会对你有所帮助,我从未尝试过,但你可以用这个逻辑进行测试。

于 2013-02-14T12:26:13.873 回答
1

如果你能够使用AFNetworking库,那就很简单了。您可以发出 HTTP 请求并使用其outputStream属性将文件下载到您的设备。假设您将下载按钮连接到功能downloadVideoFromURL:withName:

- (void)downloadVideoFromURL:(NSURL*)url withName:(NSString*)videoName
{
    //filepath to your app's documents directory
    NSString *appDocPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *videosPath = [appDocPath stringByAppendingPathComponent:@"Videos"];
    NSString *filePath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", videoName]];

    //check to make sure video hasn't been downloaded already
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        //file was already downloaded
    }

    //video wasn't downloaded, so continue
    else
    {

        //enable the network activity indicator
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

        //create a temporary filepath while downloading
        NSString *tmpPath = [videosPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-tmp.mp4", videoName]];

        //the outputStream property is the key to downloading the file
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:tmpPath append:NO];

        //if operation is completed successfully, do following
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            //rename the downloaded video to its proper name
            [[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:filePath error:nil];

            //disable network activity indicator
            [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;

            //optionally, post a notification to anyone listening that the download was successful
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadedVideo" object:nil];

        //if the operation fails, do the following:
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"error: %@", error);

            //delete the downloaded file (it is probably partially downloaded or corrupt)
            [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];

            //disable network activity indicator
            [AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
        }];

        //start the operation
        [operation start];

    }
}
于 2012-09-07T14:20:12.403 回答