【问题标题】:Create dummy audio file in iPhone在 iPhone 中创建虚拟音频文件
【发布时间】:2011-12-05 12:24:52
【问题描述】:

我想以编程方式创建一个虚拟音频文件并将其保存到磁盘,以用于涉及AVAudioPlayer 的单元测试。

在 iOS 中最简单的方法是什么?

【问题讨论】:

  • 您想以编程方式创建它吗?

标签: iphone ios unit-testing core-audio avaudioplayer


【解决方案1】:

您可以使用 AVAudioRecord 在一段时间内录制一些随机噪音。

或者,您可以使用以下链接中提供的详细信息从头开始创建一个波形文件:http://www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html

编辑:链接已失效。试试这个https://web.archive.org/web/20120701191443/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];

【讨论】:

  • 谢谢!我会玩 AVAudioRecord。但是,正如链接所示,我不太喜欢单元测试来研究 wav 标头。 :)
  • 链接不起作用……这就是为什么您应该在答案中包含要点。
  • 链接是点击诱饵?点击并转到奇怪的网站!
【解决方案2】:

一位同事提出了另一种解决方案:使用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];

【讨论】:

  • 是的,这可能是另一种更简单的方法。我以为您想以编程方式生成它,并且波形文件每次都应该不同。 :)
  • 这个方法创建的音频文件无法播放,有什么特殊原因吗?
猜你喜欢
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多