8

奇妙な問題があります。私のアプリでは、以下のコードを使用して複数のオーディオファイルとビデオファイルを組み合わせています。結果のビデオは、デバイスからコンピューターにダウンロードしてQuick Timeで再生すると正常に機能するように見えますが、UIWebViewまたはAVPLayerのいずれかを使用して新しく作成したビデオを再生しようとすると、マージされたビデオファイルの最初の部分しか表示されません。

さらに、MPMoviePlayerControllerを使用して再生しようとすると、「読み込み中」でハングします。

すべての作曲で音声が聞こえます。明確にするために、2つの配列があります。1-オーディオファイルへのパスを持つaudioPieces [song1、song2、song3]。2-ビデオファイルへのパスを持つmoviePieces[movie1、movie2、movie3]; これらのファイルをマージすると、movie1しか表示されませんが、song1 + song2+song3が聞こえます。PSの曲と映画の長さは異なります(0.2秒未満の違い)。どんな助けでもありがたいです。よろしくお願いします、ヤヌス

-(void)putFilesTogether{
AVMutableComposition *mixComposition = [AVMutableComposition composition]; 
AVMutableCompositionTrack *videoCompositionTrack =[[AVMutableCompositionTrack alloc]init];
AVMutableCompositionTrack *audioCompositionTrack =[[AVMutableCompositionTrack alloc]init];

NSLog(@" movie  %@ audio %@ ", moviePieces, audioPieces);


NSError * error;
for(int i=0;i<moviePieces.count;i++)
{
    NSFileManager * fm = [NSFileManager defaultManager];
    NSString * movieFilePath;
    NSString * audioFilePath;
    movieFilePath = [moviePieces objectAtIndex:i];
    audioFilePath = [audioPieces objectAtIndex:i];


    if(![fm fileExistsAtPath:movieFilePath]){
        NSLog(@"Movie doesn't exist %@ ",movieFilePath);
    }
    else{
        NSLog(@"Movie exist %@ ",movieFilePath);
    }

    if(![fm fileExistsAtPath:audioFilePath]){
        NSLog(@"Audio doesn't exist %@ ",audioFilePath);
    }
    else{
        NSLog(@"Audio exists %@ ",audioFilePath);
    }


   NSURL *videoUrl = [NSURL fileURLWithPath:movieFilePath];
   NSURL *audioUrl = [NSURL fileURLWithPath:audioFilePath];


    AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];
     AVAssetTrack *videoAssetTrack= [[videoasset tracksWithMediaType:AVMediaTypeVideo] lastObject];

    AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
    AVAssetTrack *audioAssetTrack= [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];

    videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    CMTime tempTime = mixComposition.duration;

    [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioasset.duration) ofTrack:audioAssetTrack atTime:tempTime error:&error];

    [videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoasset.duration) ofTrack:videoAssetTrack atTime:tempTime error:&error];

    if(error)
    {
        NSLog(@"Ups. Something went wrong! %@", [error debugDescription]);
    }
}


NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];

float ran = arc4random()%1000;
NSString * pathToSave = [NSString stringWithFormat:@"Output%@%f.mp4",caldate,ran];
pathToSave =[DOCUMENTS_FOLDER stringByAppendingPathComponent:pathToSave];
NSURL *movieUrl = [NSURL fileURLWithPath:pathToSave];

AVAssetExportSession *exporter =[[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];

exporter.outputFileType=AVFileTypeQuickTimeMovie;
exporter.outputURL=movieUrl;
exporter.shouldOptimizeForNetworkUse=YES;

CMTimeValue val = mixComposition.duration.value;

CMTime start=CMTimeMake(0, 600);
CMTime duration=CMTimeMake(val, 600);
CMTimeRange range=CMTimeRangeMake(start, duration);
exporter.timeRange=range;

[exporter exportAsynchronouslyWithCompletionHandler:^{
    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed:{
            NSLog(@"Export failed: %@ %@", [[exporter error] localizedDescription],[[exporter error]debugDescription]);
            NSString * message = @"Movie wasn't created. Try again later.";
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];
            break;}
        case AVAssetExportSessionStatusCancelled:{ NSLog(@"Export canceled");
            NSString * message1 = @"Movie wasn't created. Try again later.";
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message1 waitUntilDone:NO];
            break;}
        case AVAssetExportSessionStatusCompleted:
        {
            NSString * message = @"Movie was successfully created.";
            CMTime duration = mixComposition.duration;

            [self saveData:duration ofPath:pathToSave];
            [self cleanFiles];
            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];
        }
    }}];

}

4

1 に答える 1

12

問題は次のとおりです。

videoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

それらはforループ本体の外側に移動する必要があります。

于 2013-01-13T10:48:21.213 に答える