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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:54:49+00:00 2026-05-15T15:54:49+00:00

I’ll keep it short and sweet – I’m building an application just for practice

  • 0

I’ll keep it short and sweet – I’m building an application just for practice before I buy myself the iPhone Developer Program.

I’m experimenting with the AVFoundation.framework and I keep running into a bug which will only let me play the first sound I initialize in my code. Do you think any of you could help me out? Thanks in advance!! 🙂

view controller

-(IBAction)play {
    if (segments.selectedSegmentIndex == 0) {
        NSLog(@"Segment = 0");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path = [bundle pathForResource:@"meow" ofType:@"wav"];

        if (path != nil) {
            NSURL *url = [NSURL fileURLWithPath:path];
            AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
            [player prepareToPlay];
            [player play];
        }
    }

    else if (segments.selectedSegmentIndex == 1) {
        NSLog(@"Segment = 1");
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *path2 = [bundle pathForResource:@"meowloud" ofType:@"wav"];

        if (path2 != nil) {
            NSURL *url2 = [NSURL fileURLWithPath:path2];
            AVAudioPlayer *player2 = [[AVAudioPlayer alloc]initWithContentsOfURL:url2 error:NULL];
            [player2 prepareToPlay];
            [player2 play];
//          [player2 release];
        }
    }

    text1.textColor = [UIColor clearColor];
    text2.textColor = [UIColor clearColor];
    text3.textColor = [UIColor clearColor];
    text4.textColor = [UIColor clearColor];

}

When this code gets executed, only the meow.wav is executed, no matter which segment is selected.

  • 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-15T15:54:50+00:00Added an answer on May 15, 2026 at 3:54 pm

    Your code has a couple of memory leaks. You allocate instances of AVAudioPlayer in your play method, but you never release those instances. See Cocoa’s memory management qguidelines for details. Since the AVAudioPlayer instances need to remain in memory to play, the easiest solution is to add a member variable to your viewController class and set it up as a retain property.

    For the sake of Don’t Repeat Yourself (DRY), I would also suggest adding a method which encapsulates all of the code needed to play a single wav file.

    Here is how I would write this view controller:

    MyViewController.h

    @interface MyViewController : UIViewController
    {
        AVAudioPlayer *player;
    }
    @property(nonatomic, retain) AVAudioPlayer *player;
    - (void)playWavFile:(NSString *)fileName;
    - (IBAction)play;
    @end
    

    MyViewController.m

    @synthesize player;
    
    - (void)dealloc {
        [player release];
    }
    
    - (void)playWavFile:(NSString *)fileName {
        NSBundle *bundle = [NSBundle mainBundle];   
        NSString *path = [bundle pathForResource:fileName ofType:@"wav"];
    
        if (path != nil) {
            AVAudioPlayer *newPlayer;
            NSURL *url = [NSURL fileURLWithPath:path];
            newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                               error:NULL];
            [self setPlayer:newPlayer];
            [newPlayer release];
    
            [newPlayer prepareToPlay];
            [newPlayer play];
        }
    }
    
    - (IBAction)play {
    
        if (segments.selectedSegmentIndex == 0) {
            [self playWavFile:@"meow"];
        }
        else if (segments.selectedSegmentIndex == 1) {
            [self playWavFile:@"meowloud"];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.