Search code examples
iphoneiosunit-testingcore-audioavaudioplayer

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.

What's the simplest way to do this in iOS?


Solution

  • A colleague suggested an alternative solution: create the smallest audio file possible with Audacitiy, get its bytes, and copy them into the code.

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