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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:59:32+00:00 2026-05-22T15:59:32+00:00

I have a sound file that I use throughout my program: NSString *soundPath1 =

  • 0

I have a sound file that I use throughout my program:

NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"];
NSURL *soundFile1 = [[NSURL alloc] initFileURLWithPath:soundPath1];   
soundFilePlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile1 error:nil];
[soundFilePlayer1 prepareToPlay];   

So soundFilePlayer1 is defined in my .h file because I need it to be global.

Now… do I need to release soundFilePlayer1 and soundFile1? (and soundPath1?)

If so… where do I do this?
I think I need to release soundFilePlayer1 in: (but do I need to stop it first since it may be playing when they exit the program?)

- (void)dealloc {
   [soundFilePlayer1 release];
}

what about the other files… where do I release those?
Thanks

  • 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-22T15:59:32+00:00Added an answer on May 22, 2026 at 3:59 pm

    Remember a simple rule of thumb. Only release if you have called alloc, init, or new on the object. So, you’ll want to release soundFile1. Alternatively, you can use a convenience method and have the sound file added to the autorelease pool automatically:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
    NSURL *file = [NSURL fileURLWithPath:path]; //autoreleased
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
    
    if([player prepareToPlay]){
        [player play];
    }  
    

    And yes, you can release player (soundFilePlayer1) in dealloc, but if you’re not looping the audio file there’s a better way. Conform to the AVAudioPlayerDelegate:

    @interface YourView : UIViewController <AVAudioPlayerDelegate> { //example
    

    Then implement the method here:

    -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
        //your audio file is done, you can release safely
        [player release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple widget that plays a sound file when pressed. Right now,
I have created a voice recorder applet. The output sound file will be .wav
I have a 10 second sound effect wave file. What I would like to
I have two raw sound streams that I need to add together. For the
I have an application that starts playing a sound when user touches the uiview
Hey. I have this javascript file that I'm getting off the web and it
I'm trying to use fmodex 4.30.03 to play an MP3 file under Mono/Ubuntu. My
I have a class called Sound.java that consists of 3 functions (initSound, addSound, and
I have an input file that I want to sort based on timestamp which
I have a sound cue that I want played whenever the player does 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.