NSBundleResourceRequest bundlePath
<p><strong>[UPDATE AT THE END OF THE QUESTION]</strong></p>

<p>I use to play some audio files in my app.</p>

<p>I used to store the files in the app, but then I moved to <code>On Demand Resources</code>and <code>NSBundleResourceRequest</code>.</p>

<p>I have an <code>IBAction</code>  to play a random file, and since I moved to <code>ODR</code>, it is not working. The only thing I changed was <code>NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];</code> (line 3)</p>

<pre><code>NSBundleResourceRequest *request1;

- (IBAction)randomPlay {
    NSString *bundleRoot = [[request1 bundle] bundlePath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
    NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3'"];
    NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
    .
    .
    .
    audio = [onlyMP3s[arc4random_uniform((uint32_t)onlyMP3s.count)] 
            stringByReplacingOccurrencesOfString:@".mp3" withString:@""];
    [self playSelectedAudio:audio];
}
</code></pre>

<p>The <code>dirContents</code> looks like:</p>

<p><a href="https://i.stack.imgur.com/eNdUl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eNdUl.jpg" alt="enter image description here"></a></p>

<p>when it is supposed to contain the mp3 files.</p>

<p>What am I doing wrong with <code>bundleRoot</code>?</p>

<p>My <code>viewDidload</code> is</p>

<pre><code>- (void)viewDidLoad {
 [super viewDidLoad];

 tagsSet = [NSSet setWithObjects:@"sounds", nil];
 request1 = [[NSBundleResourceRequest alloc] initWithTags:tagsSet];
 [request1 conditionallyBeginAccessingResourcesWithCompletionHandler:^
                               (BOOL resourcesAvailable) {
    if (resourcesAvailable) {
        //don't do nothing. just use the file slater
    } else {
        [request1 beginAccessingResourcesWithCompletionHandler:^
                               (NSError * _Nullable error) {
            if (error == nil) {
                //download the files
            } else {
            }
        }];
    }
}];
</code></pre>

<p>I actually have another method to play a selected audio (not a random one)</p>

<pre><code>-(void)playSelectedAudio:(id)audioToPlay {
NSURL *url = [NSURL fileURLWithPath:[[request1 bundle] 
              pathForResource:audioToPlay ofType:@"mp3"]];
</code></pre>

<p>and it is working fine.</p>

<p>So there is a problem in the <code>bundleRoot</code>, is is not where my files are located.</p>

<p><strong>[UPDATE]</strong></p>

<p>I actually did</p>

<pre><code>- (IBAction)randomPlay {
       NSURL *bundleRoot = [NSURL fileURLWithPath:[[request1 bundle] 
                            pathForResource:@"" ofType:@"mp3"]];
       NSFileManager *fm = [NSFileManager defaultManager];
       NSArray *dirContents = [fm contentsOfDirectoryAtURL:bundleRoot 
                              includingPropertiesForKeys:[NSArray array] 
                              options:0 error:nil];
        NSPredicate *fltr = [NSPredicate predicateWithFormat:
                            @"self ENDSWITH '.mp3'"];
        NSArray *onlyMP3s = [dirContents 
                            filteredArrayUsingPredicate:fltr];
        .
        .
        .
        audio = [onlyMP3s[arc4random_uniform((uint32_t)onlyMP3s.count)] 
                        stringByReplacingOccurrencesOfString:@".mp3" 
                        withString:@""];
        [self playSelectedAudio:audio];
    }];

}
</code></pre>

<p>and it is ALMOST working but is is always playing the first audio in the directory.</p>