Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose a file randomly from Resources folder in iOS

I have one thousand .mp3 files in my Resources folder within an iOS 4.2 XCode project.

For now I can select one file from the folder ( and play it ) with the following code in my .m file: ( Given the file name of "AFile.mp3" )

   -(IBAction)playSound {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"AFile"ofType:@"mp3"];

        AVAudioPLayer* theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

        theAudio.delegate = self;
        [theAudio play];
}

I'd like to be able to have the code choose an .mp3 randomly, play it, then select another file and play them all until they have all been played at least once. I assume I will need to use an NSArray, but am uncertain on how to proceeed.

I changed the code to the following, and am not getting any errors when I run this in the iOS Simulator. No File appears to be played when I run it though? When I launch the app, it quits, but does not log any errors in the Debugger..

Updated code:

-(IBAction)playSound {

NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@".mp3" inDirectory:@"Resources"];
NSString *randomPath = [array objectAtIndex:arc4random() % [array count]];

AVAudioPlayer* theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: randomPath] error: NULL;
    theAudio.delegate = self;
    [theAudio play];

}

How can an I set up a simple array of the 1000 .mp3 files, and have the method play one at random?

like image 853
cit Avatar asked Dec 15 '25 10:12

cit


2 Answers

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsofURL: [NSURL arrayURLWithPath:array] error:NULL];

should be:

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsofURL: [NSURL URLWithString:[array objectAtIndex:11]] error:NULL];

therefore the number '11' is a random file path. To get randomness you can use this:

int randomNumber = random()%1000; // random number between 0 and 1000

and then use randomNumber instead of 11

To play each file only one time you can play let's say song number four one time and then remove this item from your array. The next time you generate a random number use 999 instead of 1000 and so on till it gets to 1.

//EDIT: try following:

-(IBAction)playSound {

    NSArray *array = [[NSBundle mainBundle] pathsForResourcesOfType:@".mp3" inDirectory:@"Resources"];
    int rndm = arc4random() % [array count];
    if (rndm<0) rndm*=-1;
    NSString *randomPath = [array objectAtIndex:rndm];

    AVAudioPlayer* theAudio =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: randomPath] error: NULL;
    theAudio.delegate = self;
    [theAudio play];

}

Use this snippet. from a previous SO answer

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:bundleRoot];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];

then get the array count.. and use random to get the random integer number and file.

like image 45
k-thorat Avatar answered Dec 17 '25 08:12

k-thorat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!