如何从youtube下载视频文件?

时间:2013-07-14 17:20:50

标签: ios objective-c youtube

Bellow我有一些代码可以完成youtube视频的下载过程。它工作一次,然后第二次不工作,所以我把这行代码放在[videoData retain];,这停止了我收到的线程错误。但是,一旦添加[videoData retain];我的下载器下载两次而不是一次下载第二个视频,下载第一个视频时就不会这样做...

这是我的代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        videoData = [[NSMutableData alloc] init];

    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [videoData appendData:data];

    }

    //when downloading done
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        filePath = [NSString stringWithFormat:@"%@/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]];
        [videoData writeToFile:filePath atomically:YES];
        [videoData retain];

        [videoData release];


        NSLog(@"LOCAL VIDEO LINK %@",filePath);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Downloading complete"
                                                       message: @"Download complete. You have successfully downloaded your video from youtube"
                                                      delegate: self
                                             cancelButtonTitle:@"Go Back"
                                             otherButtonTitles:@"OK",nil];

        [alert show];
        [alert release];


    }
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
            NSLog(@"user pressed Button Indexed 0");
            // Any action can be performed here
            [movieController stop];
            [movieController.view removeFromSuperview];
            bar.hidden = YES;
            texturl.text = nil;
            hidden.hidden = YES;
            videoData = nil;




        }
        else
        {
            NSLog(@"user pressed Button Indexed 1");
            // Any action can be performed here
        }
    }

提前致谢

0 个答案:

没有答案