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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:58:02+00:00 2026-05-24T02:58:02+00:00

I currently have background music playing in my application. in: -(void)viewDidLoad I start the

  • 0

I currently have background music playing in my application.
in:

-(void)viewDidLoad

I start the music, using:

 [self LoadMusic];

This basically starts up music buffers it then if the settings state that ‘Sound is On’ the music starts.

The problem i have is when i move views, the music continues to play (as it should) but when you go back to the view controller where the music was first started, it actives again.

So you end up with the same music looping on top of each other.

I have tried a few things such as:

if(MusicPlay.playing){
// do nothing
}else{
// start music
  [self LoadMusic];
}

This is the code for loadMusic

- (void) LoadMusic{
    //Notification for stoping
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopMusic) name:@"StopMusic" object:nil];
    //Notification for playing
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMusic) name:@"PlayMusic" object:nil];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *SoundSwitch = [defaults stringForKey:@"Sound_EnabledS"];  


    // grab the path to the caf file
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: @"Menu_Loop"
                                    ofType: @"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    // create a new AVAudioPlayer initialized with the URL to the file
    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: nil];
    // set our ivar equal to the new player
    self.MusicPlay = newPlayer;
    // preloads buffers, gets ready to play
    [MusicPlay prepareToPlay];
    MusicPlay.numberOfLoops = -1; // Loop indefinately
    if ([SoundSwitch isEqualToString:@"1"]){
        [self.MusicPlay play];  // Plays the sound
    }else{
        [self.MusicPlay stop];  // Stops the sound
    }
}

But this does not work as it does not see MusicPlay.playing is only true once the view has been ‘restarted’

If anyone has a good way to solved this problem.
Please let me know.

Thank you.

  • 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-24T02:58:03+00:00Added an answer on May 24, 2026 at 2:58 am

    Your code should also work but check with following code

    if([self.MusicPlay isPlaying]){ // playing is property and isPlaying is getter method
    // do nothing
    }else{
    // start music
      [self LoadMusic];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We currently have an application that starts its own background services to manage syncing
I'm developing an iPhone game and currently using AVAudioPlayer for playing background music and
Background I currently have an array like this: [1,1,2,3,4,5,5,5,6,7,8,8,8,8,9,10] I have been using a
Background Currently, I am working on a Rails application. I have different products that
I currently have an application that naturally gets pushed to the background when the
Background : I currently have a Web Forms, ASP.NET 3.5/C# application which I'm interested
I currently have a complete cover background image. Here is the code: #back{ /*
Background Info: File Replication is Lame Currently, we have a massive, high-traffic ASP.NET web
I have a website that I'm making that needs a background resizer. Currently, I
I have this query which works correctly in MySQL. More background on it here

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.