fabdurso
fabdurso

Reputation: 2444

NSBundleResourceRequest bundlePath

[UPDATE AT THE END OF THE QUESTION]

I use to play some audio files in my app.

I used to store the files in the app, but then I moved to On Demand Resourcesand NSBundleResourceRequest.

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

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];
}

The dirContents looks like:

enter image description here

when it is supposed to contain the mp3 files.

What am I doing wrong with bundleRoot?

My viewDidload is

- (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 {
            }
        }];
    }
}];

I actually have another method to play a selected audio (not a random one)

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

and it is working fine.

So there is a problem in the bundleRoot, is is not where my files are located.

[UPDATE]

I actually did

- (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];
    }];

}

and it is ALMOST working but is is always playing the first audio in the directory.

Upvotes: 1

Views: 971

Answers (1)

Borys Verebskyi
Borys Verebskyi

Reputation: 4268

Didn't you forget beginAccessingResourcesWithCompletionHandler: call to actually download resources?

- (IBAction)randomPlay {
    NSBundleResourceRequest *request1; // I hope you properly initialize this request :)
    [request1 beginAccessingResourcesWithCompletionHandler:^(NSError * _Nullable error) {
        if (error != nil) {
            // Probably, add here dispatch_async(dispatch_get_main_queue(), ^{
            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];
            .
            .
            .
            [self playSelectedAudio:audio];
            // You will probably need to invoke [request1 endAccessingResources] later, when you finish accessing your audio.
        }
        else {
            // handle error
            [request1 endAccessingResources];
        }
    }
}

Several important notes from NSBundle.h:

The completion block will be invoked on a non-main serial queue when the resources are available or an error has occurred.

So you will probably need to use additional dispatch_async to main queue in completion handler. But this depends solely on your code.

Be sure to always invoke the -endAccessingResources method to balance a call to the begin method, even in the case of an error in the completion handler.

Upvotes: 1

Related Questions