IOS video download problems (attached AFHTTPRequestOperation video download method)

Source: Internet
Author: User

IOS video download problems (attached AFHTTPRequestOperation video download method)

Preface:

A recent instant chat project involves uploading and downloading videos. AFNetworking uploads images and videos (multiple images are supported)

Some problems occur during video download. The video cannot be played after download.

 

1. Video Download Method

 

 

/*** Download file */-(void) downloadFileURL :( NSString *) aUrl savePath :( NSString *) aSavePath fileName :( NSString *) aFileName tag :( NSInteger) aTag {NSFileManager * fileManager = [NSFileManager defaultManager]; // check whether the local file already exists NSString * fileName = [NSString stringWithFormat: @ % @/% @, aSavePath, aFileName]; // check whether the attachment exists if ([fileManager fileExistsAtPath: fileName]) {NSData * audioData = [NSData dataWithContentsOfFile: f IleName]; //... video download completed operation} else {// create attachment storage directory if (! [FileManager fileExistsAtPath: aSavePath]) {[fileManager createDirectoryAtPath: aSavePath attributes: YES attributes: nil error: nil];} // Download Attachment NSURL * url = [[NSURL alloc] initWithString: aUrl]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest: request]; operation. inputStream = [NSInputStream inputStreamWithURL: url]; operation. outputStream = [NSOutputStream outputStreamToFileAtPath: fileName append: NO]; // download progress control/* [operation setDownloadProgressBlock: ^ (NSUInteger bytesRead, long totalBytesRead, long timeout) {NSLog (@ is download: % f, (float) totalBytesRead/totalBytesExpectedToRead) ;}]; * // download completed [operation setCompletionBlockWithSuccess: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSData * audioData = [NSData dataWithContentsOfFile: fileName]; // sets the download data to the res dictionary object and returns the downloaded data using a proxy NSData //... video download completed} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {// download failed //... failed video download operation}]; [operation start];}

2. After downloading the video, the file will not be suffixed with. MP4, And I will definitely add a suffix of. MP4 in the current filename, and the playback will be OK.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.