ios - 如何在新项目中使用 ExtAudioFileConvert 示例
我想要的只是读取一个 caf 文件并将其写入另一个文件,希望我可以对其进行操作,例如将多个文件合并为一个文件或修剪。所以我找到了苹果自己的iPhoneExtAudioFileConvertTest Sample,看起来很有挑战性,但我想一旦我把这些片段放到我自己的项目中,我就可以玩它了,事实证明这是相当麻烦的。
现在,经过几个不眠之夜的信息挖掘和大量的试验和错误运行; 我相信我已经成功地在我自己的项目中使用了 iPhoneExtAudioFileConvertTest 示例。我想我发布这篇文章是为了让其他编码员省点睡眠。
步骤:
- 将 iPhoneExtAudioFileConvertTest 示例中的 iPublicUtility 文件夹和 ExtAudioFileConvert.cpp 文件添加到我的项目中。
- 将 3 个新 .cpp 文件的扩展名重命名为 .mm(.h 文件保持不变)。这允许 xcode 将 cpp 文件视为目标 c++ 文件。
- ExtAudioFileConvert.mm
- iPublicUtility/CAStreamBasicDescription.mm
- iPublicUtility/CAXException.mm
- 在我的 myViewController.m (UIViewController) 类中,计划调用
DoConvertFile(sourceURL, destinationURL, outputFormat, SampleRate);
方法,我添加了一行以使 DoConvertFile 方法可见extern OSStatus DoConvertFile(CFURLRef sourceURL, CFURLRef destinationURL, OSType outputFormat, Float64 outputSampleRate);
- 还将 myViewController.m 重命名为 myViewController.mm,因为它使用 .cpp 文件进行 ConvertFile 方法调用
- 在我的项目设置中的 Targets / Build Settings 下,我搜索了键“Header Search Paths”并添加了以下值/Developer/Extras/CoreAudio/PublicUtility/ $(inherited) 如果没有这行,编译器会给我超过 30 个错误,例如 'DebugPrintfFile' 未在此范围内声明*
- 我注意到该示例在应用程序委托中也有代码
- 所以我直接将示例的appDelegate中的2个方法的代码复制到我自己的appDelegate中,即
静态 void InterruptListener(void *inClientData, UInt32 inInterruption)
static void propertyListener(void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData)
- 并在原始
#import
语句之后添加了以下 4 行- #import“CAXException.h”
extern void ThreadStateInitalize();
extern void ThreadStateBeginInterruption();
extern void ThreadStateEndInterruption();
- 当然,还将示例
applicationDidFinishLaunching
中的代码添加到我的didFinishLaunchingWithOptions
- 并再次将我的 appDelegate.m 重命名为 appDelegate.mm
- 所以我直接将示例的appDelegate中的2个方法的代码复制到我自己的appDelegate中,即
现在,当我编译时,我没有收到任何错误,只有在注释掉 ExtAudioFileConvert.mm 中的断言时,运行时才有效。
// in this sample we should never be on the main thread here
assert(![NSThread isMainThread]);
另外,在启动时,我收到一个错误,该错误不会导致任何问题,但我更愿意删除它。有人吗?
Error: couldn't set audio category (-50)
这似乎来自我的 appDelegate 中的此调用
// our default category -- we change this for conversion and playback appropriately
UInt32 audioCategory = kAudioSessionCategory_SoloAmbientSound;
XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "couldn't set audio category");
除了这些警告/错误之外,我可以编写/转换 caf 文件:)。 如果有任何反馈,我将不胜感激。
All I wanted was to read a caf file and write it to another file, in the hopes that perhaps I can manipulate it such as combine multiple files into one, or trimming. So I found Apples's own iPhoneExtAudioFileConvertTest Sample, which looks very challenging but I figured once I take the pieces into my own project, then I can play with it, this proved to be rather cumbersome.
Now, after a few sleepless nights of digging for information and lots trial and errors runs;
I believed I have managed to use the the iPhoneExtAudioFileConvertTest Sample in my own project. I thought I would post this to save a fellow coder some sleep.
Steps:
- Added the iPublicUtility folder and the ExtAudioFileConvert.cpp file from the iPhoneExtAudioFileConvertTest sample to my project.
- Renamed the extension of the 3 new .cpp files to .mm (the .h files stay the same). This allows xcode to view the cpp files as objective c++ files.
- ExtAudioFileConvert.mm
- iPublicUtility/CAStreamBasicDescription.mm
- iPublicUtility/CAXException.mm
- In my myViewController.m (UIViewController) class that plans to call the
DoConvertFile(sourceURL, destinationURL, outputFormat, sampleRate);
method, I added one line to make the DoConvertFile method visibleextern OSStatus DoConvertFile(CFURLRef sourceURL, CFURLRef destinationURL, OSType outputFormat, Float64 outputSampleRate);
- Also renamed myViewController.m to myViewController.mm because it's using a .cpp file to make the convertFile method call
- In my project setting under Targets / Build Settings, I searched for the key "Header Search Paths" and added the following for it's value /Developer/Extras/CoreAudio/PublicUtility/ $(inherited) without this line the compiler would give me over 30 errors, such as 'DebugPrintfFile' was not declared in this scope*
- I noticed that the sample also has code in the app delegate
- so I copied the code of the 2 methods directly from the sample's appDelegate into my own appDelegate, namly
static void interruptionListener(void *inClientData, UInt32 inInterruption)
static void propertyListener(void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData)
- and added the the following 4 lines right after the original
#import
statement- #import "CAXException.h"
extern void ThreadStateInitalize();
extern void ThreadStateBeginInterruption();
extern void ThreadStateEndInterruption();
- of course also added the code from the samples
applicationDidFinishLaunching
into mydidFinishLaunchingWithOptions
- and once again renamed my appDelegate.m to appDelegate.mm
- so I copied the code of the 2 methods directly from the sample's appDelegate into my own appDelegate, namly
Now when I compile I get no errors and runtime only works if I comment out the assert in ExtAudioFileConvert.mm
// in this sample we should never be on the main thread here
assert(![NSThread isMainThread]);
Also at startup I receive an error which is not causing any issues, but I'd prefer to remove it. Anyone ?
Error: couldn't set audio category (-50)
which seems to be coming from this call in my appDelegate
// our default category -- we change this for conversion and playback appropriately
UInt32 audioCategory = kAudioSessionCategory_SoloAmbientSound;
XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "couldn't set audio category");
Other than these warnings/errors, I can write/convert a caf file :).
I would appreciate any feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论