【问题标题】:when i export the video its not played当我导出视频时它没有播放
【发布时间】:2011-03-29 06:21:08
【问题描述】:

在这里,我成功创建了视频,我将视频和音频合并为 MOV 格式,并通过使用 AVAssetExportSession 导出文件,但是当在媒体播放器中播放文件时,它只显示黑屏

这里我附上了视频和音频的合并代码

-(void)combine:(NSString *)audiopathvalue videoURL:(NSString *)videopathValue;
{

   // 1. Create a AVMutableComposition

    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent(); //Debug purpose - used to calculate the total time taken
    NSError *error = nil;
    AVMutableComposition *saveComposition = [AVMutableComposition composition];


  //  2. Get the video and audio file path  
    NSString *tempPath = NSTemporaryDirectory();
    NSString *videoPath = videopathValue ;//<Video file path>;
    NSString *audioPath = audiopathvalue ;//<Audio file path>;;


    //3. Create the video asset 
    NSURL * url1 = [[NSURL alloc] initFileURLWithPath:videoPath];
    AVURLAsset *video = [AVURLAsset URLAssetWithURL:url1 options:nil];
    [url1 release];

   // 4. Get the AVMutableCompositionTrack for video and add the video track to it.
//        The method insertTimeRange: ofTrack: atTime: decides the what portion of the video to be added and also where the video track should appear in the final video created.
        AVMutableCompositionTrack *compositionVideoTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVAssetTrack *clipVideoTrack = [[video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [video duration]) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
    NSLog(@"%f %@",CMTimeGetSeconds([video duration]),error);



    //5. Create the Audio asset 

    NSLog(@"audioPath:%@",audioPath);
    NSURL * url2 = [[NSURL alloc] initFileURLWithPath:audioPath];
    AVURLAsset *audio = [AVURLAsset URLAssetWithURL:url2 options:nil];
    [url2 release];

    //6. Get the AVMutableCompositionTrack for audio and add the audio track to it.
        AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration]) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
    NSLog(@"%f %@",CMTimeGetSeconds([audio duration]),error);

    //7. Get file path for of the final video.
        NSString *path = [tempPath stringByAppendingPathComponent:@"mergedvideo.MOV"];
    if([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }

    NSURL *url = [[NSURL alloc] initFileURLWithPath: path];


    //8. Create the AVAssetExportSession and set the preset to it.
    //The completion handler will be called upon the completion of the export.
    AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] autorelease];
    exporter.outputURL=url;
    exporter.outputFileType = @"com.apple.quicktime-movie";
    NSLog(@"file type %@",exporter.outputFileType);
    exporter.shouldOptimizeForNetworkUse = YES;






    [exporter exportAsynchronouslyWithCompletionHandler:^{

        switch ([exporter status]) {

            case AVAssetExportSessionStatusFailed:

                NSLog(@"Export failed: %@", [[exporter error] localizedDescription]);
                NSLog(@"ExportSessionError: %@", exporter.error);

                break;

            case AVAssetExportSessionStatusCancelled:

                NSLog(@"Export canceled");

                break;

            case AVAssetExportSessionStatusCompleted:
            {
                NSLog(@"Export Completed");
                ImageToAirPlayAppDelegate *theApp_iphone=(ImageToAirPlayAppDelegate *)[[UIApplication sharedApplication] delegate];
                [theApp_iphone call];
                break;
            }

            default:
                break;
        }

        //[exporter release];

    }];

在视频路径中,它包含一系列图像 并且在音频路径中只有一个音频

【问题讨论】:

    标签: iphone objective-c video audio


    【解决方案1】:

    函数(不在您的代码中):

    - (void) captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
    

    尝试在那里进行处理。

    为您提供您必须在混音中使用的 outputFileURL。没有理由在函数 combine 中使用 NSString。

    我还建议您使用 AVFileTypeQuickTimeMovie 代替“com.apple.quicktime-movie”。如果您想尝试其他格式,则相同但更容易处理。

    要了解可用的格式,只需使用

    NSLog(@"%@", [exporter supportedFileTypes]);
    

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多