【发布时间】:2013-09-10 10:34:33
【问题描述】:
我想从 url 下载视频。我有视频网址,但不知道如何下载。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
strFileName = @"MyVideo";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.youtube.com/v/P9MeXRzKUuA?version=3&f=videos&app=youtube_gdata"]]; //strFileURL is url of your video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease];
[conection start];
[request release];
strFilePath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName] retain];
NSLog(@"file path = %@",strFilePath);
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
NSLog(@"file path = %@",strFilePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:strFilePath]) {
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
}
file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
if (file) {
[file seekToEndOfFile];
}
NSLog(@"response received");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
if (file) {
[file seekToEndOfFile];
}
[file writeData:receivedata];
NSLog(@"data received");
}
[responseData appendData:receivedata];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
NSLog(@"response = %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
[file closeFile];
NSLog(@"file closed");
NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
NSLog(@"fileurl = %@",fileURL);
[webView loadRequest:[NSURLRequest requestWithURL:fileURL]];
}
在connectiondidfinishedloading 方法中,当我打印响应时它为空。
请帮帮我
【问题讨论】:
-
您需要搜索“YoutubeParser”类,这将帮助您完成这个简单的 NSMutableURLRequest 不适用于 youtube 视频下载
-
技术上一个简单的
NSURLRequest就足以下载它,但您需要解析器来获取实际的视频网址。请记住,从 youtube 下载是一种很快就会被苹果拒绝的方法。 -
我尝试使用 HCYoutubeParser 类。但它不播放视频。你知道任何 youtube 解析器类的名称或链接吗