如何获取 iPhone 上资源的文件系统路径?
在 iPhone 上,我需要获取资源的路径。 好的,完成了,但是当涉及到 CFURLCreateFromFileSystemRepresentation 的时候,我只是不知道如何解决这个问题。 为什么会出现这个错误? 任何解决方案或解决方法将受到高度赞赏。 先感谢您。
为了在 iPhone 上使用 AudioQueue 播放音频,我查看了以下示例: SpeakHere、AudioQueueTools(来自 SimpleSDK 目录)和 AudioQueueTest。 我试着做这做那,试图把这些谜题拼在一起。 现在,我被困在这个问题上。 由于上面的 sndFile 抛出异常,程序崩溃了。
我正在使用 AVAudioPlayer 来播放 iPhone 游戏中的所有声音。 在真正的 iPhone 设备上,当播放声音时,结果非常滞后,所以我决定需要使用 AudioQueue。
- (id) initWithFile: (NSString*) argv{
if (self = [super init]){
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:argv
ofType:@"mp3"];
int len = [soundFilePath length];
char* fpath = new char[len];
//this is for changing NSString into char* to match
//CFURLCreateFromFileSystemRepresentation function's requirement.
for (int i = 0; i < [soundFilePath length]; i++){
fpath[i] = [soundFilePath characterAtIndex:i];
}
CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
(NULL, (const UInt8 *)fpath, strlen(fpath), false);
if (!sndFile) {
NSLog(@"sndFile error");
XThrowIfError (!sndFile, "can't parse file path");
}
}
On the iPhone I need to get the path for the resource. OK, done that, but when it comes to CFURLCreateFromFileSystemRepresentation thing, I just don't know how to solve this. Why does this error occur? Any solution or workaround would be highly appreciated. Thank you in advance.
I have taken a look at the following examples in order to play audio using AudioQueue on the iPhone:
SpeakHere, AudioQueueTools (from the SimpleSDK directory) and AudioQueueTest. I tried do this and that, trying to put the puzzles together. Right now, I am stuck at this. The program crashed because of the exception thrown from the sndFile above.
I am using AVAudioPlayer to play every sound on my iPhone games. On the real iPhone device, it turned out to be very laggy when a sound is to be played, so I decided I need to use AudioQueue.
- (id) initWithFile: (NSString*) argv{
if (self = [super init]){
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:argv
ofType:@"mp3"];
int len = [soundFilePath length];
char* fpath = new char[len];
//this is for changing NSString into char* to match
//CFURLCreateFromFileSystemRepresentation function's requirement.
for (int i = 0; i < [soundFilePath length]; i++){
fpath[i] = [soundFilePath characterAtIndex:i];
}
CFURLRef sndFile = CFURLCreateFromFileSystemRepresentation
(NULL, (const UInt8 *)fpath, strlen(fpath), false);
if (!sndFile) {
NSLog(@"sndFile error");
XThrowIfError (!sndFile, "can't parse file path");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要 CFURL?
如果您在其他地方有需要 CFURL 的方法,那么借助免费桥接,您可以简单地使用 NSURL。 因此,要创建 NSURL,您只需执行以下操作:
一般来说,如果您发现自己使用 CF 对象,那么您可能做错了什么。
Why do you need a CFURL?
If you have a method elsewhere that requires a CFURL, you can simply use an NSURL thanks to toll-free bridging. So to create the NSURL, you'd just do:
In general, if you find yourself using CF objects you are probably doing something wrong.
我不确定这是否会消除您的异常,但有一种更简单的方法可以将
NSString
转换为char
数组。 以下是我编写此方法的方法:或者,由于
CFURL
与NSURL
是“免费桥接”,因此您可以简单地执行以下操作:I'm not sure if this will get rid of your exception, but there is a simpler way to convert an
NSString
to an array ofchar
. Here is how I would write this method:Or, since
CFURL
is "toll-free bridged" withNSURL
, you can simply do: