在我的新应用程序中,我需要从不同的网站下载视频,假设它是视频下载器应用程序。为此,我计划在 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),这样我就可以 paas 这个来运行。我需要了解那个 URL
最佳答案
如果您能够使用 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];
}
}
关于iphone - 从 HTML + UIWebView 下载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12319278/