This drives me nuts - CFBundleCopyResourceURL returns NULL

Discussion in 'iOS Programming' started by AZAstro, Sep 22, 2012.

  1. AZAstro, Sep 22, 2012
    Last edited by a moderator: Sep 22, 2012

    macrumors newbie

    Joined:
    Sep 22, 2012
    #1
    It's gotta be a bug I guess.

    The following code in a very simple app. The top one works fine and the mirror below it doesn't..
    Code:
    // This method works fine.
    - (IBAction)playHorn:(id)sender {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Horn", CFSTR ("aiff"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }
    
    //  This doesn't work for some reason because the function CFBundleCopyResourceURL line passes NULL back.
    - (IBAction)play60m4a:(id)sender {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"60", CFSTR ("m4a"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
    }
    
    ****
    
    20348#($*(*@_)(*)(*
    
    Suggestions welcome. I am NOT an ace at objective C programming so when these things fail like this it drives me nuts.

    The two files, 60.m4a and Horn.aiff both play fine in iTunes and when selected in the xCode navigator.
     
  2. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #2
    The quick fix would be to convert your @"60" file to an aiff file in iTunes. I think I read that in the new simulator it doesn't play mp3s (could be true for other file types).

    Nick
     
  3. thread starter macrumors newbie

    Joined:
    Sep 22, 2012
    #3
    I tried that and - no joy. And another thing - I've had some aif files NOT PLAY and others play fine, which suggests it may have something to do with the files themselves.
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    I recommend that you stay away from CF functions as much as possible. Do you know what Copy means in that function name? Use objective-C to build your NSURL and NSStrings and use typecasts when needed to satisfy the compiler.

    Anyway, the code you show looks simple enough and correct. Either the sounds are present in your app bundle, or not. And either the sounds are in the right format to be played, or not. If you get NULL when trying to get the resource url then you know what that means. If the resource URL is valid but the sound doesn't play then you know what that means.

    Unfortunately some sounds play on the device and not the Sim, and the reverse.

    Some notes from my code:

    // The most compatible sound file format for iPhone is caf.
    // Microsoft WAV PCM 16 bit also works.
    // It's possible to convert to caf with:
    // afconvert -f caff -d LEI16@44100 MYSOUND.WAV
    // this will make it single channel:
    // afconvert -f caff -d LEI16@44100 -c 1 MYSOUND.WAV
     
  5. thread starter macrumors newbie

    Joined:
    Sep 22, 2012
    #5
    Thank you very much for your comments.

    I am getting a null back from the call to the bundle so I guess I'll have to address that first. I have been doing some internet-wide searches and not finding much on this issue.

    Trying to put some final touches on an app but the playing of the sounds has been problematic...
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If you're really getting back NULL then that's a simple problem. I recommend you start by deleting your app from the Sim and any devices. Option-clean the build folder. Then rebuild.

    If the problem persists then what does the Build phases say for these files? Are they really in your target?
     
  7. thread starter macrumors newbie

    Joined:
    Sep 22, 2012
    #7
    Great Idea! I'll give this a try and will advise...
     
  8. thread starter macrumors newbie

    Joined:
    Sep 22, 2012
    #8
    That was it. Those files didn't show up in the bundle. Dragging and dropping the files into the project navigator is supposed to do that automatically, but it didn't do its job. BUG!@

    I added the files manually (in BUILD PHASES) and then everything worked fine.

    Thanks for a great suggestion. It was easy once I was headed in the right direction!
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    It does, if you have the "Copy items into destination group's folder (if needed)" option checked. I doubt you found a bug with Xcode.
     

    Attached Files:

Share This Page