Commit 88b5f37f authored by yuzheng.tz's avatar yuzheng.tz

优化视频导出方法,当发生错误时调用失败回调透出错误信息

parent 3d1a48df
...@@ -81,7 +81,9 @@ ...@@ -81,7 +81,9 @@
- (void)getVideoWithAsset:(id)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler completion:(void (^)(AVPlayerItem *, NSDictionary *))completion; - (void)getVideoWithAsset:(id)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler completion:(void (^)(AVPlayerItem *, NSDictionary *))completion;
/// Export video 导出视频 /// Export video 导出视频
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion; - (void)getVideoOutputPathWithAsset:(id)asset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure;
/// Deprecated, Use -getVideoOutputPathWithAsset:failure:success:
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion __attribute__((deprecated("Use -getVideoOutputPathWithAsset:failure:success:")));
/// Get photo bytes 获得一组照片的大小 /// Get photo bytes 获得一组照片的大小
- (void)getPhotosBytesWithArray:(NSArray *)photos completion:(void (^)(NSString *totalBytes))completion; - (void)getPhotosBytesWithArray:(NSArray *)photos completion:(void (^)(NSString *totalBytes))completion;
......
...@@ -714,7 +714,7 @@ static dispatch_once_t onceToken; ...@@ -714,7 +714,7 @@ static dispatch_once_t onceToken;
#pragma mark - Export video #pragma mark - Export video
/// Export Video / 导出视频 /// Export Video / 导出视频
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion { - (void)getVideoOutputPathWithAsset:(id)asset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
if ([asset isKindOfClass:[PHAsset class]]) { if ([asset isKindOfClass:[PHAsset class]]) {
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init]; PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal; options.version = PHVideoRequestOptionsVersionOriginal;
...@@ -724,16 +724,21 @@ static dispatch_once_t onceToken; ...@@ -724,16 +724,21 @@ static dispatch_once_t onceToken;
// NSLog(@"Info:\n%@",info); // NSLog(@"Info:\n%@",info);
AVURLAsset *videoAsset = (AVURLAsset*)avasset; AVURLAsset *videoAsset = (AVURLAsset*)avasset;
// NSLog(@"AVAsset URL: %@",myAsset.URL); // NSLog(@"AVAsset URL: %@",myAsset.URL);
[self startExportVideoWithVideoAsset:videoAsset completion:completion]; [self startExportVideoWithVideoAsset:videoAsset success:success failure:failure];
}]; }];
} else if ([asset isKindOfClass:[ALAsset class]]) { } else if ([asset isKindOfClass:[ALAsset class]]) {
NSURL *videoURL =[asset valueForProperty:ALAssetPropertyAssetURL]; // ALAssetPropertyURLs NSURL *videoURL =[asset valueForProperty:ALAssetPropertyAssetURL]; // ALAssetPropertyURLs
AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
[self startExportVideoWithVideoAsset:videoAsset completion:completion]; [self startExportVideoWithVideoAsset:videoAsset success:success failure:failure];
} }
} }
- (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset completion:(void (^)(NSString *outputPath))completion { /// Deprecated, Use -getVideoOutputPathWithAsset:failure:success:
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion {
[self getVideoOutputPathWithAsset:asset success:completion failure:nil];
}
- (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
// Find compatible presets by video asset. // Find compatible presets by video asset.
NSArray *presets = [AVAssetExportSession exportPresetsCompatibleWithAsset:videoAsset]; NSArray *presets = [AVAssetExportSession exportPresetsCompatibleWithAsset:videoAsset];
...@@ -747,7 +752,7 @@ static dispatch_once_t onceToken; ...@@ -747,7 +752,7 @@ static dispatch_once_t onceToken;
NSDateFormatter *formater = [[NSDateFormatter alloc] init]; NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"]; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"];
NSString *outputPath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/output-%@.mp4", [formater stringFromDate:[NSDate date]]]; NSString *outputPath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/output-%@.mp4", [formater stringFromDate:[NSDate date]]];
NSLog(@"video outputPath = %@",outputPath); // NSLog(@"video outputPath = %@",outputPath);
session.outputURL = [NSURL fileURLWithPath:outputPath]; session.outputURL = [NSURL fileURLWithPath:outputPath];
// Optimize for network use. // Optimize for network use.
...@@ -757,6 +762,9 @@ static dispatch_once_t onceToken; ...@@ -757,6 +762,9 @@ static dispatch_once_t onceToken;
if ([supportedTypeArray containsObject:AVFileTypeMPEG4]) { if ([supportedTypeArray containsObject:AVFileTypeMPEG4]) {
session.outputFileType = AVFileTypeMPEG4; session.outputFileType = AVFileTypeMPEG4;
} else if (supportedTypeArray.count == 0) { } else if (supportedTypeArray.count == 0) {
if (failure) {
failure(@"该视频类型暂不支持导出", nil);
}
NSLog(@"No supported file types 视频类型暂不支持导出"); NSLog(@"No supported file types 视频类型暂不支持导出");
return; return;
} else { } else {
...@@ -775,25 +783,38 @@ static dispatch_once_t onceToken; ...@@ -775,25 +783,38 @@ static dispatch_once_t onceToken;
// Begin to export video to the output path asynchronously. // Begin to export video to the output path asynchronously.
[session exportAsynchronouslyWithCompletionHandler:^(void) { [session exportAsynchronouslyWithCompletionHandler:^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
switch (session.status) { switch (session.status) {
case AVAssetExportSessionStatusUnknown: case AVAssetExportSessionStatusUnknown: {
NSLog(@"AVAssetExportSessionStatusUnknown"); break; NSLog(@"AVAssetExportSessionStatusUnknown");
case AVAssetExportSessionStatusWaiting: } break;
NSLog(@"AVAssetExportSessionStatusWaiting"); break; case AVAssetExportSessionStatusWaiting: {
case AVAssetExportSessionStatusExporting: NSLog(@"AVAssetExportSessionStatusWaiting");
NSLog(@"AVAssetExportSessionStatusExporting"); break; } break;
case AVAssetExportSessionStatusExporting: {
NSLog(@"AVAssetExportSessionStatusExporting");
} break;
case AVAssetExportSessionStatusCompleted: { case AVAssetExportSessionStatusCompleted: {
NSLog(@"AVAssetExportSessionStatusCompleted"); NSLog(@"AVAssetExportSessionStatusCompleted");
dispatch_async(dispatch_get_main_queue(), ^{ if (success) {
if (completion) { success(outputPath);
completion(outputPath); }
} break;
case AVAssetExportSessionStatusFailed: {
NSLog(@"AVAssetExportSessionStatusFailed");
if (failure) {
failure(@"视频导出失败", session.error);
}
} break;
case AVAssetExportSessionStatusCancelled: {
NSLog(@"AVAssetExportSessionStatusCancelled");
if (failure) {
failure(@"导出任务已被取消", nil);
} }
});
} break; } break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed"); break;
default: break; default: break;
} }
});
}]; }];
} }
} }
......
...@@ -496,12 +496,13 @@ ...@@ -496,12 +496,13 @@
_selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]]; _selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]]; _selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
// open this code to send video / 打开这段代码发送视频 // open this code to send video / 打开这段代码发送视频
// [[TZImageManager manager] getVideoOutputPathWithAsset:asset completion:^(NSString *outputPath) { [[TZImageManager manager] getVideoOutputPathWithAsset:asset success:^(NSString *outputPath) {
// NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath); NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
// Export completed, send video here, send by outputPath or NSData // Export completed, send video here, send by outputPath or NSData
// 导出完成,在这里写上传代码,通过路径或者通过NSData上传 // 导出完成,在这里写上传代码,通过路径或者通过NSData上传
} failure:^(NSString *errorMessage, NSError *error) {
// }]; NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
}];
[_collectionView reloadData]; [_collectionView reloadData];
// _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH)); // _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment