【发布时间】:2011-12-05 12:24:52
【问题描述】:
我想以编程方式创建一个虚拟音频文件并将其保存到磁盘,以用于涉及AVAudioPlayer 的单元测试。
在 iOS 中最简单的方法是什么?
【问题讨论】:
-
您想以编程方式创建它吗?
标签: iphone ios unit-testing core-audio avaudioplayer
我想以编程方式创建一个虚拟音频文件并将其保存到磁盘,以用于涉及AVAudioPlayer 的单元测试。
在 iOS 中最简单的方法是什么?
【问题讨论】:
标签: iphone ios unit-testing core-audio avaudioplayer
您可以使用 AVAudioRecord 在一段时间内录制一些随机噪音。
或者,您可以使用以下链接中提供的详细信息从头开始创建一个波形文件:http://www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html
NSString *path = [[NSBundle mainBundle] pathForResource:@"WavHeader" ofType:@"wav"];
NSMutableData *audioData = [[NSMutableData alloc] initWithContentsOfFile:path];
int 样本 = 22050;
uint8_t dataToRecord[样本];
int x;
for(x = 0; x
// populate the "dataToRecord" array with audio data. //
}
[audioData appendBytes:(const void *)dataToRecord length:samples];
// Get path to documents folder and set a file name //
[audioData writeToFile:pathToFile atomically:YES];
【讨论】:
一位同事提出了另一种解决方案:使用Audacitiy 创建尽可能小的音频文件,获取其字节,然后将它们复制到代码中。
这是结果:
const char bytes[] = {0x52, 0x49, 0x46, 0x46, 0x26, 0x0, 0x0, 0x0, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x44, 0xac, 0x0, 0x0, 0x88, 0x58, 0x1, 0x0, 0x2, 0x0, 0x10, 0x0, 0x64, 0x61, 0x74, 0x61, 0x2, 0x0, 0x0, 0x0, 0xfc, 0xff};
NSData* data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSString* filePath = [path_ stringByAppendingPathComponent:@"test.wav"];
[data writeToFile:filePath atomically:YES];
audioURL_ = [NSURL fileURLWithPath:filePath];
【讨论】: