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.