2010-10-23 49 views
1

我尝试将.mp3文件添加到AVMutableCompositionTrack,之后我想导出新文件。问题是:生成的文件在导出后存在,但它是空的,无法播放。有人在我的代码中看到错误吗?AVAssetExportSession - AVMutableCompositionTrack - 导出的输出文件为空

AVMutableComposition *saveComposition = [AVMutableComposition composition]; 
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *tempPath = [docpaths objectAtIndex:0]; 

NSLog(@"Temporary Path: %@", tempPath); 

NSString *audioPath = [[NSBundle mainBundle] pathForResource: @"1" ofType: @"mp3"]; 
NSURL *audioUrl = [[NSURL alloc] initFileURLWithPath:audioPath]; 
AVURLAsset *audio = [AVURLAsset URLAssetWithURL:audioUrl options:nil]; 
NSLog(@"%@", audio); 
[audioUrl release]; 
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]; 

NSString *path = [tempPath stringByAppendingPathComponent:@"mergedaudio.m4a"]; 
if([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{ 
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; 
} 
NSURL *url = [[NSURL alloc] initFileURLWithPath: path]; 

AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetAppleM4A] autorelease]; 
exporter.outputURL=url; 
[exporter setOutputFileType:@"com.apple.m4a-audio"]; 

NSLog(@"%@", [exporter supportedFileTypes]); 
exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0]; 

[exporter exportAsynchronouslyWithCompletionHandler:^{ 

}]; 

预先感谢您!

+0

啊哈,它与使用的文件格式有关。例如,如果我在(例如)文件中创建了所有的东西(源文件,目标文件, WAV格式,那么它的作品!必须尝试更多,并将结果发布... – Micko 2010-10-23 16:59:56

回答

2

就像我在评论中写到的那样,它与不同的文件格式有关。我将文件更改为.m4a以及代码 - 因此,所有内容(此操作的来源和目标)都与.m4a相关并且可以工作。顺便说一句:我也尝试使用.wav文件,但在使用wav进行操作时会发生奇怪的事情。我不推荐它。

+0

我想在Nsdata中保存歌曲数据以便上传到服务器,您可以帮助吗? – GhostRider 2010-10-27 09:17:00

+1

对不起,我从来没有使用NSData或服务器连接... – Micko 2010-10-31 20:31:23