Resolved [[NSBundle mainBundle] URLForResource:file extension:_extension] returns nil

Discussion in 'iOS Programming' started by moonman239, Feb 5, 2014.

  1. moonman239, Feb 5, 2014
    Last edited: Feb 6, 2014

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Code:

    AVQueuePlayer+initWithAppAudioFiles.m
    Code:
    #import "AVQueuePlayer+initWithAppAudioFiles.h"
    
    @implementation AVQueuePlayer (initWithAppAudioFiles)
    +(AVQueuePlayer *)initWithAppFiles:(NSArray *)fileNames extension:(NSString *)extension
    {
        NSMutableArray *fileURLs;
        for (NSString *fileName in fileNames)
        {
            NSURL *fileURL = [[NSBundle mainBundle] URLForResource:fileName withExtension:extension];
            [fileURLs addObject:fileURL];
        }
        AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] initWithItems:fileURLs];
        return queuePlayer;
    }
    @end
    
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    Looks like the problem lies with the following code:

    Code:
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:fileName withExtension:extension];
    
    Even though the file names and extensions are correct, fileURL remains nil.
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    OK, it turns out that the audio file was not located under "Copy Build Resources," which is located under "Build Phases." This is why URLForResource returned nil. Thanks, Zaph from Stack Overflow!
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    How did you add the files to your project? When using the File > Add Files to "ProjectName"... option (or dragging-and-dropping), you should make sure you have checked the proper target in the "Add to targets" section of the dialog. If so, this will automatically add the file to the Copy Bundle Resources settings.
     

Share This Page