2011-08-08 55 views
0

Im使用AVURLAsset.Im同步音頻和視頻文件獲取導出的文件,但只有音頻輸出,而不是視頻。如何解決此問題。請幫助我。提前感謝。AVURLAsset不工作iphone

進出口使用下面的代碼:

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 ([[NSFileManager 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]; 
      } 

      } 


     ];     
      } 

回答

2
  1. 簡化代碼的smallest working sample顯示的問題。
  2. 檢查返回錯誤。
  3. 嘗試使用其他導出預設而不是AVAssetExportPresetPassthrough
+0

請指出是否適用於iphone – Senorina

+0

您的意思是出口預設?是的,他們在iPhone上工作。我不確定是否所有這些,但'AVAssetExportPresetHighestQuality'都適合我。 – zoul

+0

可以給我們發送工作示例代碼 – Senorina

0

嘗試不同的出口預置代替AVAssetExportPresetPassthrough 和的NSString * videoName = @ 「export.mp4」 代替的NSString * videoName = @ 「export.m4v」;