最近在做一个音频的处理程序。从一段音频中截取几段然后拼接在一起。分割我是用的AVURLAsset写的。目前是能分割出来,然后拼接我是用
// 大于xM分片拼接xM
if (fileSizeB > KFILESIZE) {
// 分片
long long pieces = fileSizeB /KFILESIZE; // 整片
long long let = fileSizeB %KFILESIZE; // 剩余片
long long sizes = pieces;
// 有余数
if (let > 0) {
// 加多一片
sizes += 1;
}
NSFileHandle *handleB = [NSFileHandle fileHandleForReadingAtPath:filePathB];
for (int i =0; i < sizes; i++) {
[handleB seekToFileOffset:i * KFILESIZE];
NSData *tmp = [handleB readDataOfLength:KFILESIZE];
[handleA writeData:tmp];
}
[handleB synchronizeFile];
// 大于xM分片读xM(最后一片可能小于xM)
}else{
[handleA writeData:[NSData dataWithContentsOfFile:filePathB]];
}
这一段百度来的代码写的。我是用两段沙盒下的音频来试验,拼接后的效果是只能播放第一段,但是如果是拿工程中的两段音频来拼接是可以拼接的。。然后我就卡住了想不明白。。求助求助。
然后百度还有一个拼接的方法是转成nsdata拼写。这种方法拼工程里的音频也是可以的,拼沙盒路径下的音频显示的nsdata的大小也确实是两段音频相加的总和。但是播放和时长啥啥的确实就只有第一段音频的数据
心好累。。想不明白。跪求解答。

解决方案 »

  1.   

    我这边有一段音频合并的代码,不知道有没有用。
    - (NSString *)filePathWithName:(NSString *)filename
    {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            return [documentsDirectory stringByAppendingPathComponent:filename];
    }- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        //音频文件路径
            NSString *mp3Path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
            NSString *mp3Path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"];
            //音频数据
            NSData *sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1];
            NSData *sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2];
            //合并音频
            NSMutableData *sounds = [NSMutableData alloc];
            [sounds appendData:sound1Data];
            [sounds appendData:sound2Data];
            //保存音频
    //        [[NSFileManager defaultManager] createFileAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp.mp3"] 
    //                                                                                        contents:sounds 
    //                                                                                  attributes:nil];
            NSLog(@"data length:%d", [sounds length]);        [sounds writeToFile:[self filePathWithName:@"tmp.mp3"] atomically:YES];
            
            [window makeKeyAndVisible];
        
        return YES;
    }