Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3628208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:59:11+00:00 2026-05-18T23:59:11+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:59:12+00:00Added an answer on May 18, 2026 at 11:59 pm
    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];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have over a thousand folders, each folder contains one or more files with
I have a to select a number between one to thousand... I dont want
I have one text input and one button (see below). How can I use
I have one application which uses the standard .NET forms authentication cookie, now I
I have one table that contain more that 40 thousand record when I retrieve
Greetings, I have one file - more or less a greylisting file. I need
I have a table now containing over 43 million records. To execute SELECT ,
I have developed one SaaS based application on Django. During searching more than thousand
I have 2 products, one is a thousand dollars so for it's price I
We have an iPhone app with several thousand users, and we've had one or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.