【问题标题】:iPhone programming, cannot find resource at path?iPhone编程,找不到路径资源?
【发布时间】:2011-03-08 16:34:03
【问题描述】:

我想知道你们中是否有人可以帮助解决这个问题。下面的代码尝试在 char *path 中确定并包含的路径位置访问音频文件“4sSweepReverse.wav”。这段代码在模拟器上运行完全没问题,但是在iphone上运行就不行了?

//print out the file path
NSLog(@"FilePath: ");
NSLog(filePath);


//Cast filePath from NSString, to Char array pointer. Needed to get reference to audio file.
const char *path = [filePath cStringUsingEncoding:[NSString defaultCStringEncoding]];

//get a ref to the audio file, need one to open it
CFURLRef audioFileURL   = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *) path, strlen (path), false);

//open the audio file
OSStatus result = AudioFileOpenURL (audioFileURL, 0x03, kAudioFileWAVEType, &mAudioFile);

//were there any errors reading? if so deal with them first
if (result != noErr) {
    NSLog([NSString stringWithFormat:@"Could not open file: %s", filePath]);
    packetCount = -1;
}

我知道文件中包含的路径是正确的。模拟器上的路径是

/Users/sam2/Library/Application Support/iPhone Simulator/4.2/Applications/A355770A-6942-4E1F-BB8D-8A1B1906B42C/audioRecorder.app/4sSweepReverse.wav

iPhone 上的路径是

/var/mobile/Applications/2273E045-0A63-439E-A6AC-FD85CD30F7F2/audioRecorder.app/4sSweepReverse.wav

但是当调试在 iPhone 上运行的应用程序时,我得到 -43 错误(或找不到文件)?

如果有人可以提供帮助,那就太棒了! 山姆

【问题讨论】:

    标签: ios4 resources ios-simulator


    【解决方案1】:

    哇,我现在感觉自己像个白痴!感谢您的所有建议,事实证明 iPhone 正在查找该文件,但我使用权限标志“0x03”打开它,这是读/写权限,显然作为资源,我只能使用“0x01”访问它只读权限。

    下面是正确的代码行:

        //open the audio file
    OSStatus result = AudioFileOpenURL (audioFileURL, 0x01, kAudioFileWAVEType, &mAudioFile);
    

    再次感谢您的帮助!!

    【讨论】:

    • +1:太好了!您可能希望使用预定义的枚举 kAudioFileReadPermission 来帮助避免丢失此类内容。
    【解决方案2】:

    项目中的资源是否准确命名为“4sSweepReverse.wav”而不是(例如)“4ssweepreverse.wav”(全小写)?

    设备文件系统区分大小写,但模拟器不区分大小写。
    请参阅 Apple QA1697:Why doesn't my device load a file that loads fine in the Simulator?

    【讨论】:

    • 感谢您的回复,是的,资源的名称正好是 '4sSweepReverse.wav' 这就是我无法理解的原因?
    • filePath 实际是如何设置的? NSLogs 在 sim 和设备上的确切输出是什么?
    • filePath 在这里设置:'[sineSweep open:[[NSBundle mainBundle] pathForResource:@"1sSweepReverse" ofType:@"wav" inDirectory:nil]];'这将打开一个新的类实例 (InMemoryAudioFile),这是发布的原始代码所在的位置。原始帖子中显示的路径是直接从在 sim 和设备上以调试模式运行的控制台复制/粘贴的。
    • EIDT:文件路径设置如下:'[sineSweep open:[[NSBundle mainBundle] pathForResource:@"1ssweepreverse" ofType:@"wav"]];'
    【解决方案3】:

    您可能想尝试的一些事情 -

    • 删除、重新添加资源
    • 干净的构建。
    • 重置 iPhone 模拟器 使用更简单或至少不同的名称,直到您可以让它工作, 可能是a.wav之类的东西?不确定资源的副本是否缓存在某处。
    • 也不确定是否以数字开头文件名对其有任何影响(可能不会,但到底是什么,不妨试一试)

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 2016-06-15
      • 2021-12-26
      • 2014-10-20
      • 2012-12-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多