2014-01-30 1 views
10

here으로 표시된 기술을 사용하여 일부 오디오 파일을 연결하려고합니다. 내 오디오 파일은 .m4a이며 Quicktime에서 정상적으로 작동하는지 확인할 수 있습니다. 여기에 내가 그들을 연결하는 사용하려고 해요 코드는 다음과 같습니다 눈치코코아 : 파일에서로드 된 AVAsset에 0 트랙이 있습니다.

[currFile.audioContent writeToFile:tempOldFilePath atomically:NO]; 

    AVURLAsset *oldAudioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempOldFilePath] options:nil]; 
    AVURLAsset *newAudioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempInputFilePath] options:nil]; 

    NSLog(@"oldAsset num tracks = %lu",(unsigned long)oldAudioAsset.tracks.count); 
    NSLog(@"newAsset num tracks = %lu",(unsigned long)newAudioAsset.tracks.count); 
    AVAssetTrack *oldTrack = [[oldAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    AVAssetTrack *newTrack = [[newAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 

    AVMutableComposition *mutableComposition = [AVMutableComposition composition]; 

    AVMutableCompositionTrack *compTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

    NSError *error=nil; 
    [compTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, oldTrack.timeRange.duration) ofTrack:oldTrack atTime:kCMTimeZero error:&error]; 
    if (error) { 
     NSLog(@"%@",error.localizedDescription); 
     error=nil; 
    } 
    [compTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, newTrack.timeRange.duration) ofTrack:newTrack 
         atTime:oldTrack.timeRange.duration error:&error]; 

    if (error) { 
     NSLog(@"%@",error.localizedDescription); 
     error=nil; 
    } 

    exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetAppleM4A]; 


    exporter.outputURL = [NSURL URLWithString:tempCompFilePath]; 

    exporter.outputFileType = AVFileTypeAppleM4A; 

    [exporter exportAsynchronouslyWithCompletionHandler:^{ 
     NSLog(@"handller"); 
     NSError *error=nil; 
     NSData *newData = [NSData dataWithContentsOfFile:tempCompFilePath options:0 error:&error]; 
     NSLog(@"%lu",(unsigned long)newData.length); 
     if (error) { 
      NSLog(@"%@",error.localizedDescription); 
     } 

     currFile.audioContent = newData; 
     [[AppDelegate sharedDelegate] saveAction:nil]; 

    }]; 

첫 번째 문제는 수출의 핸들러 메소드가 호출되지 않습니다 것입니다. 그 이유는 다음과 같습니다. URL에서 AVAssets를 생성 한 후 로그 문에 0 개의 트랙이 포함되어 있음을 알 수 있습니다. Apple의 예는 AVAssets의로드 방식을 정확하게 보여주지 않습니다.

이 방법을 얻는 방법에 대한 조언이 있으십니까?

+0

사용하려는 OS X 버전은 무엇입니까? –

답변

17

이미 발견 했으므로 URLWithString:이 아니라 fileURLWithPath:을 사용해야 URL을 만들 수 있습니다.

URLWithString:@"file:///path/to/file" 또는 @"http://example.com/"과 같이 URL을 설명하는 문자열을 필요로합니다. 문자열이 @"/path/to/file"과 같이 경로 만 설명하면 fileURLWithPath:을 사용해야하며 누락 된 부분을 정확하게 채 웁니다. 더 기술적으로

, URLWithString: (예 : HTTP로만 경로 하지만 당신은 모든 파일 지향 방식으로 기본 URL에 비해 사용에 갈 수있는 특별한 계획와 단순히 URL로 경로를 해석합니다 GET /path/to/file). fileURLWithPath:은 경로를 로컬 파일 경로로 해석하고 이에 따라 file: URL을 반환합니다.

4

분명히 잘못된 NSURL 메서드를 사용하고있었습니다. 이를 다음과 같이 변경했습니다 :

AVURLAsset *oldAudioAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempOldFilePath] options:nil]; 

이제 내 AVAssets에는 각각 1 개의 트랙이 있습니다. 왜 이것이 필요한지에 관해 누구나 좋은 설명을 해줄 수 있다면, 그 대답을 받아 들일 것입니다.

5

이 오류가 발생한 이유를 발견하고 결국 해결했습니다.

원래 소스 파일 경로를 ".mp4"로 설정했습니다. 녹음 된 동영상 파일의 형식이 MOV이므로 " .mov"로 변경되었습니다. 대신

NSString *source_file_path = @"temp_video.mp4" 

문제의

NSString *source_file_path = @"temp_video.mov" 

고정되었고 지금은 잘 작동된다.

모두에게 도움이되기를 바랍니다.

관련 문제