AVURLAsset not working iphone
Im syncing the audio and video files using AVURLAsset.Im getting the exported file but only audio is exported and not the video.How to solve this issue.please help me.Thanks in advance.
Im using the code below:
moviePlayer = [[VideoPlay alloc]initWithNibName:@"VideoPlay" bundle:nil];
if(sp==1){
NSURL *VUrl = [NSURL URLWithString:elements.videoUrl];
NSURL *AUrl = [NSURL URLWithString:elements.audioUrl1 ];
NSLog(@"%@--%@",AUrl,VUrl);
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:AUrl options:nil];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:VUrl options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetPassthrough];
NSString *videoName = @"export.m4v";
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSLog(@"Export Status %d-- ", _assetExport.status);
_assetExport.outputFileType = @"com.apple.quicktime-movie";
NSLog(@"file type %@",_assetExport.outputFileType);
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
NSLog(@"hello");
switch (_assetExport.status)
{
case AVAssetExportSessionStatusFailed:
{
NSLog (@"FAIL %@",_assetExport.error);
if ([[NSFile开发者_如何学CManager defaultManager] fileExistsAtPath:[_assetExport.outputURL path]])
{
[[NSFileManager defaultManager] removeItemAtPath:[_assetExport.outputURL path] error:nil];
}
// // [self performSelectorOnMainThread:@selector (ritenta)
// withObject:nil
// waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCompleted:
{
// // [self performSelectorOnMainThread:@selector (saveVideoToAlbum:)
// withObject:exportPath
// waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCancelled:
{
NSLog (@"CANCELED");
break;
}
}
NSLog(@"Export Status %d-- %@", _assetExport.status, _assetExport.outputURL);
if(_assetExport.status==3){
moviePlayer.videolink = _assetExport.outputURL;
[self presentModalViewController:moviePlayer animated:YES];
[moviePlayer readyPlayer];
}
}
];
}
- Simplify your code to the smallest working sample that shows the problem.
- Check the return errors.
- Try a different export preset instead of
AVAssetExportPresetPassthrough
.
Try a different export preset instead of AVAssetExportPresetPassthrough and NSString *videoName = @"export.mp4" instead of NSString *videoName = @"export.m4v";
精彩评论