iPhone:用于编辑的音频文件的NSData表示 [英] iPhone: NSData representation of Audio file for Editing

查看:111
本文介绍了iPhone:用于编辑的音频文件的NSData表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很久以前一直在摸不着头脑,但没有绕过这个。我还没有找到一个音频编辑的例子!我想在原始音频文件中的某个地方插入新的音频文件,将其保存为新转换的音频文件。

I have been scratching my head since long now but not getting around to this. I haven't found a single example for Audio editing! I want to insert new Audio file in between somewhere in original Audio file, save it as new converted audio files.

为此,我编写了以下代码。我从这里

For this I have written following code. I got this idea from here.

NSString *file1 = [[NSBundle mainBundle] pathForResource:@"file1" ofType:@"caf"]; // Using PCM format
NSString *file2 = [[NSBundle mainBundle] pathForResource:@"file2" ofType:@"caf"];

NSData *file1Data = [[NSData alloc] initWithContentsOfFile:file1];
NSData *file2Data = [[NSData alloc] initWithContentsOfFile:file2];

NSMutableData *mergedData =[[NSMutableData alloc] initWithCapacity:([file1Data length] + [file2Data length])];

// First chunk from original sound file 
NSRange firstChunk;
firstChunk.length = startingPosition;
firstChunk.location = 0;
[mergedData appendData:[file1Data subdataWithRange:firstChunk]];

// Add new sound
[mergedData appendData:file2Data];

// First chunk from original sound file 
NSRange secondChunk;
secondChunk.length = [file1Data length] - startingPosition;
secondChunk.location = startingPosition;
[mergedData appendData:[file1Data subdataWithRange:secondChunk]];

NSLog(@"File1: %d, File2: %d, Merged: %d", [file2Data length], [file2Data length],   [mergedData length]);

// Convert mergedData to Audio file
[mergedData writeToFile:[self filePath:@"converted.caf"] atomically:YES];

[file1Data release];
[file2Data release];
[mergedData release];

使用以下代码播放转换后的声音文件:

Using following code to play converted sound file:

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] 
                          initWithContentsOfURL:[NSURL URLWithString:[self filePath:@"converted.caf"]]  error:nil];
[audioPlayer prepareToPlay];
[audioPlayerr play];

转换的文件无法播放。进一步研究这个我发现将声音转换为NSData截断声音页眉/页脚。是对的吗?请有人帮我解决这个问题。

The converted file doesn't play. Further researching on this I found that converting sound to NSData truncates sound header/footer. Is that right? Could anyone please help me to get this sorted out please.

谢谢。

推荐答案

我认为最好的办法是使用ExtAudioFileRef将每个文件的音频样本提取到缓冲区中。

I think your best bet would be to use an ExtAudioFileRef to extract the audio samples of each file into buffers.

然后你可以自由地将样本混合到一起从你喜欢的任何位置,使用ExtAudioFileWriteAsync将新的混合文件保存到磁盘。

Then you can freely mix the samples together to and from whatever position you like and save the new mixed file to disk using ExtAudioFileWriteAsync.

例如:
将声音1提取到缓冲区A.
将声音2提取到缓冲区B中。

For example: Extract sound 1 into buffer A. Extract sound 2 into buffer B.

创建缓冲区C,其大小为A的长度加上B的长度。
然后使用ExtAudioFileWriteAsync以任何格式将该缓冲区写入磁盘。

Create buffer C whose size is A's length plus B's length. Then write that buffer to disk in whatever format you want using ExtAudioFileWriteAsync.

该文件应该可由AVAudioPlayer播放。

That file should then be playable by AVAudioPlayer.

这篇关于iPhone:用于编辑的音频文件的NSData表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆