问题
I have to record audio on an iPhone that will play back on Windows Media Player, using no non-standard codecs; that is, using no codecs that don't already ship with WMP. (Corporate requirement.) The audio has to go base-64 to a server, so I need to get the audio size as small as possible. Even at 8Kb recording frequency, AIFF files take ginormous amounts of space. I can't seem to find a compressed AIFF format that WMP will swallow.
回答1:
Best bet is to write WAV files with ulaw or alaw compression (the mFormatId
of your AudioStreamBasicDescription
would be kAudioFormatULaw
or kAudioFormatALaw
. Test to see which one gives you the smallest file sizes for your application.
Here's some code to set up an ExtAudioFile instance to write ulaw:
NSArray * docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docDir = [docs objectAtIndex:0];
NSURL * outFile = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"OutFile.wav"]];
AudioStreamBasicDescription asbd;
bzero(&asbd, sizeof(asbd));
asbd.mSampleRate = 11025; // or whatever
asbd.mFramesPerPacket = 1;
asbd.mChannelsPerFrame = 1;
asbd.mFormatID = kAudioFormatULaw; //or kAudioFormatALaw
ExtAudioFileRef theFile;
err = ExtAudioFileCreateWithURL((CFURLRef)outFile,
kAudioFileWAVEType,
&asbd,
0,
kAudioFileFlags_EraseFile,
&theFile);
Hope that helps.
--- ADDED ---
Here are modifications to the SpeakHere sample code to get ulaw compression:
SpeakHereController.mm:116
recordFilePath = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.wav"];
SpeakHereController.mm:158
recorder->startRecord(CFSTR("recordedFile.wav"));
AQRecorder.mm:192
SetupAudioFormat(kAudioFormatULaw);
AQRecorder.mm:215
XThrowIfError(AudioFileCreateWithURL(url, kAudioFileWAVEType, &mRecordFormat, kAudioFileFlags_EraseFile,
&mRecordFile), "AudioFileCreateWithURL failed");
回答2:
Although a long time since the last post, I found the answer to the problem user196004 was having not being able to save to a folder OTHER than the temporary folder.
AudioFileCreateWithURL takes a CFURLRef as input. If the url is created using CFURLCreateWithString, and the destination folder has any spaces in it, the creation of the url will fail (null).
The solution is to escape the string.
CFStringRef fileNameEscaped = CFURLCreateStringByAddingPercentEscapes( NULL, fileName, NULL, NULL, kCFStringEncodingUTF8 );
来源:https://stackoverflow.com/questions/1688284/iphone-compressed-audio-formats-that-will-play-on-windows-media-player