简体   繁体   English

在iPhone中创建虚拟音频文件

[英]Create dummy audio file in iPhone

I woud like to programatically create and save to disk a dummy audio file to use in unit tests involving AVAudioPlayer . 我想以编程方式创建一个虚拟音频文件并将其保存到磁盘中,以用于涉及AVAudioPlayer单元测试中。

What's the simplest way to do this in iOS? 在iOS中最简单的方法是什么?

You can either record some random noise for some duration using AVAudioRecord. 您可以使用AVAudioRecord在一段时间内记录一些随机噪音。

Alternatively, you can create one wave file from scratch using details given in following link : http://www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html 或者,您可以使用以下链接中给出的详细信息从头开始创建一个wave文件: http : //www.iphonedevsdk.com/forum/iphone-sdk-development/45613-creating-audio-programmatically.html

A colleague suggested an alternative solution: create the smallest audio file possible with Audacitiy , get its bytes, and copy them into the code. 一位同事提出了另一种解决方案:使用Audacitiy创建可能的最小音频文件,获取其字节,然后将其复制到代码中。

This is the result: 结果如下:

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM