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 8860337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:14:49+00:00 2026-06-14T15:14:49+00:00

I’ve keyboard like app and I’m adding each new AVAudioPlayer to NSMutableArray so I

  • 0

I’ve keyboard like app and I’m adding each new AVAudioPlayer to NSMutableArray so I can overlay sounds.
My problem is, that delagate function

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

is not fired if there are keystrokes in short interval. If I stroke for example in 0.5s or longer interval, everything works fine. Any idea why?
I want to free memory and delete object from array when his sound play is finished.

UPDATE

.h

@interface .. <AVAudioPlayerDelegate>
{
    NSMutableArray *soundObjects;
}

@property (nonatomic,strong) AVAudioPlayer *audioPlayer;

.m

-(void)playSound {

    //
    //  play sound
    //

    _audioPlayer = [self loadWav:_soundID];
    _audioPlayer.delegate = self;

    [soundObjects addObject:_audioPlayer];
    AVAudioPlayer *temp = [soundObjects objectAtIndex:soundIndex];
    soundIndex = [soundObjects count];
    [temp play];
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
#ifdef DEBUG_SOUND
        NSLog(@"FLAG");
        soundIndex--;
        [soundObjects removeObjectAtIndex:soundIndex];
#endif
}

- (AVAudioPlayer *)loadWav:(NSString *)filename {
    NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"mp3"];
    NSError * error;

    AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

    if (!player) {
#ifdef DEBUG_MODE
        NSLog(@"Error loading %@: %@", url, error.localizedDescription);
#endif
    } else {
        [player prepareToPlay];
    }
    return player;
}
  • 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-06-14T15:14:51+00:00Added an answer on June 14, 2026 at 3:14 pm

    There are several examples of NSQueue in the developer documentation for a quick example I will use an adaptation from an Obj C Recipe book I have put together using my own work.

    //This method detatches the instruction from the main thread
    -(void)keyStrokeSelector:(NSString*)objectSound{
    
    
           [NSThread detachNewThreadSelector:@selector(keyStroke:objectSound)
                                    toTarget:self
                                  withObject:nil];
    
    
     }
    
    //this selector will acctually execute the task
    -(void)keyStroke:(NSString *)objectSound
    {
       @autoreleasepool {
          //code to play the sound
       }
    }
    

    I hope that this helps. A few notes on the above code: you must use an autoreleasepool for each individual thread or Reference Counting (MRC or ARC) will not work because it only works within a autoreleasepool, the code that happens in the thread off the main thread should be coded like anything else it will simply execute and run to completion. If you have an infinite loop in a child thread it may not be noticeable while testing your app, however the OS will kill the app after ten minutes if I remember the current max time for a background thread correctly. More importantly and I cannot emphasize this enough if you do not create a thread specific @autoreleasepool then your objects will not be released and will eat the memory footprint even if you do not create a new object inside the new thread, when they are passed in they will gain a retain count and be maintained in memory.

    I hope this helps, best of luck in your program

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

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.