Choose a file randomly from the Resources folder in iOS

I have a thousand .mp3files in my folder Resourcesin an iOS 4.2 Xcode project.

Now I can select one file from the folder (and play it) with the following code in my file .m: (Given the file name "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 would like the code to randomly select .mp3, play it, then select another file and play them all until they play at least once. I assume that I will need to use NSArray, but I'm not sure how to execute the procedure.

I changed the code to the next, and I get no errors when I run this in iOS Simulator. If I run it, the file does not play? When I launch the application, it exits, 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 I set up a simple array of 1000 .mp3files and use the method randomly?

+3
source share
5 answers
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc]initWithContentsofURL: [NSURL arrayURLWithPath:array] error:NULL];

it 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 random, you can use this:

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

and then use randomNumberinstead11

, , , . , , 999 1000 , 1.

//EDIT: :

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

}
+3

. SO

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

.. .

+3

, , , :

NSString *randomPath = [array objectAtIndex:arc4random_uniform([array count])];

, arc4random() , rand()

+1

, Apple, . , , . , , , "."

NSArray * audioPathArray = [[NSBundle mainBundle] pathsForResourcesOfType: @ "mp3" inDirectory: nil];

NSLog(@"%d",[audioPathArray count]);

NSString *path = [audioPathArray objectAtIndex:arc4random() % [audioPathArray count]];

NSURL *randomPath = [[NSURL alloc]initFileURLWithPath:path]; 

AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:randomPath error:NULL];


[theAudio play];

, . , .

+1

"" " ". [NSBundle mainBundle].

:

NSString * bundlePath = [NSBundle mainBundle] bundlePath];
NSString * resourceFolderPath = [NSString stringWithFormat:@"%@/Resources", bundlePath];
NSArray * resourceFiles = [[NSFileManager defaultManager] directoryContentsAtPath:resourceFolderPath error:nil];

resourceFiles .

NSInteger randomFileIndex = arc4random() % [resourceFiles count];
NSString * randomFile = [resourceFiles objectAtIndex:randomFileIndex];
0
source

Source: https://habr.com/ru/post/1783228/


All Articles