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

The Archive Base Latest Questions

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

This is the code I’m using now, and it’s not working (nothing happens when

  • 0

This is the code I’m using now, and it’s not working (nothing happens when I press the button that calls this method). Previously, I had a property for audioPlayer and it worked (all the audioPlayers below were self.audioPlayer obviously). The problem was that when I tried to play the sound twice, it would end the first sound playing.

This is no good because I’m making a soundboard and want sounds to be able to overlap. I thought I could just make audioPlayer a local variable instead of a property and all would be ok but now the sound doesn’t work at all and I can’t figure out why. In all tutorials I’ve found for AVAudioPlayer, a property is made but no one explains why. If this can’t work, what alternatives do I have to make sounds that can overlap?

- (void)loadSound:(NSString *)sound ofType:(NSString *)type withDelegate:(BOOL)delegate {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:sound
                                     ofType:type]];

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
if (delegate) audioPlayer.delegate = self;
[audioPlayer prepareToPlay];

[audioPlayer play];
}
  • 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:46:55+00:00Added an answer on June 14, 2026 at 3:46 pm

    The reason you need a property or ivar is for the strong reference it provides. When using ARC any object without a strong pointer to it is fair game for deallocation, and in fact that is what you are seeing.

    You are also correct that an AVAudioPlayer strong pointer will only allow one audio player to be referenced at a time.

    The solution, if you choose to continue to use AVAudioPlayer is to use some sort of collection object to hold strong reference to all the player instances. You could use an NSMutableArray as shown here:

    Edit I tweaked the code slightly so method that plays the sound takes an NSString soundName parameter.

    @synthesize audioPlayers = _audioPlayers;
    -(NSMutableArray *)audioPlayers{
        if (!_audioPlayers){
            _audioPlayers = [[NSMutableArray alloc] init];
        }
        return _audioPlayers;
    }
    -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
        [self.audioPlayers removeObject:player];
    }
    -(void)playSoundNamed:(NSString *)soundName{
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:soundName
                                             ofType:@"wav"]];
        AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        if (audioPlayer){
            [audioPlayer setDelegate:self];
            [audioPlayer prepareToPlay];
            [audioPlayer play];
            [self.audioPlayers addObject:audioPlayer];
        }
    }
    

    Generally an AVAudioPlayer is overkill for an sound-effect/soundboard application. For quick sound “drops” you will likely find the audio toolbox framework, as outlined in my answer to this question.

    From looking at the System Sound class reference, it seems like you
    can only play one sound at a time.

    It can only play one SystemSoundID at a time. So for example if you have soundOne and soundTwo. You can play soundOne while soundTwo is playing, but you cannot play more than one instance of either sound at a time.

    What’s the best way to be able to play sounds that can overlap while
    still being efficient with the amount of code and memory?

    Best is opinion.

    If you need two instances of the same sound to play at the same time, then I would say the code posted in this answer would be the code to use. Due to the fact that each overlapping instance of the same sound requires creating a new resource, code like this with its audioPlayerDidFinishPlaying: is much more manageable(the memory can easily be reclaimed).

    If overlapping instances of the same sound are not a deal-breaker then I think just using AudioServicesCreateSystemSoundID() to create one instance of each sound is more efficient.

    I definitely would not try to manage the creation of and disposal of SystemSoundIDs with each press of a button. That would go wrong in a hurry. In that instance AVAudioPlayer is the clear winner on just maintainability alone.

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

Sidebar

Related Questions

This code has no errors in it, but when i click the button nothing
This code is working now. LogCat only for lead back problems from other ppl
This code is the core of a much larger script that works great in
This code is not pretty, but I want to print out the last generated
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
This code works really well for an initial insert into a table. But now
This code throws parse error, which I do not understand why. function t(){ return
this code works fine but my problem is that i need to read the
This code does not seem to run well: http://jsfiddle.net/5GEru/1/ var left = 0; function
This code always returns true. This gets called when a Login button is pressed.

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.