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

The Archive Base Latest Questions

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

How to stop background music when changing views? I have no clue. If i

  • 0

How to stop background music when changing views? I have no clue. If i press a button which takes me to a new view, there is new background music. But the old background music (which goes in an infinite loop) keeps on going. Please help! also sample some code please, here is mine:

- (void)viewDidLoad {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
    AVAudioPlayer* theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
    theAudio.numberOfLoops = -1;

    [super viewDidLoad];
}

I just need to know how to make the background music from the new view stop playing. And vice versa when i press the back button from the new view

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

    Create a property for the AVAudioPlayer *theAudio so you can access the audioPlayer from any point in your class.

    Header file of viewController

    ... 
    AVAudioPlayer *theAudio;
    ...
    @property (nonatomic, retain) AVAudioPlayer *theAudio;
    

    Implentation file of viewController

    ...
    @synthesize theAudio;
    ...
    
    - (void)viewDidLoad {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
        self.theAudio= [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]] autorelease];
        theAudio.delegate = self;
        [theAudio play];
        theAudio.numberOfLoops = -1;
    
        [super viewDidLoad];
    }
    

    If viewWillDisappear is called you can then just stop the audio with

    - (void)viewWillDisappear
    {
        [theAudio stop];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some videos in a site, which has music playing in the background.
i am also facing same problem .. by doing this background music stop but
I'm creating pages for a small web-based game which must have some background music
i have background music when the view is open then the music is start
I made a service that play background music, but when the music finished I
For my Android app I would like background music. It doesn't have to go
I my application I have a background music that plays when the program starts.
In my application background music is controlled by an asynctask. When the home button
I have two problems. 1- I am using following code to add background music
I have a small app which runs in background doing some tasks which basically

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.