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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:43:49+00:00 2026-05-23T03:43:49+00:00

I have played a sound in classA, and does anyone know how to stop

  • 0

I have played a sound in classA, and does anyone know how to stop it in classB?

I have read several posts already, most of them just mention about creating an instance (i.e. things like Class A *a in .h, and a =[[Class A alloc]init] in .m). This wont work for some reasons.

Here are some codes:
In classA.m

path1 = [[NSBundle mainBundle]  pathForResource:[@"songName" ofType:@"mp3"];

av1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path1] error:NULL];
[av1 play];

In classB.m,

a = [[classA alloc] initWithNibName:nil bundle:nil];

 [a.av1 stop];
  • 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-23T03:43:50+00:00Added an answer on May 23, 2026 at 3:43 am

    What you’re doing here,

    a = [[classA alloc] initWithNibName:nil bundle:nil];
    [a.av1 stop]; 
    

    is wrong. You’re creating a completely new object which is all probability isn’t playing any music and sending a stop message to its player. If you want to stop the player in the other class, you must store an assigned reference of the other class. If you wish to keep them independent, you can look at notifications. This is a definitive guide from Apple. Basically, this will involve registering A as the observer to a notification and then when B is ready to play it will post the notification that it’s about to play music. When A receives that notification should turn off its music.

    So in the init of A, register yourself as an observer,

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(turnOffMusic:) 
                                                 name:@"BWillPlayMusicNotification"
                                               object:nil];
    

    and then when the B object is ready to play music, post the notification,

    [[NSNotificationCenter defaultCenter] postNotification:@"BWillPlayMusicNotification"];
    

    This will result in A’s turnOffMusic being invoked which will pretty much do,

    - (void)turnOffMusic:(NSNotification *)notification {
        [self.av1 stop];
    }
    

    Remember to stop listening to notifications when the object is deallocated,

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    This approach allows you to keep both classes independent.

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

Sidebar

Related Questions

I have a sound cue that I want played whenever the player does a
I have played around with Scala for a while now, and I know that
I have played with the idea of using a wiki (MediaWiki) to centralize all
I have played around with smart phone development (windows ce), and it seemed pretty
I have played for a while writing XPath but am unable to come up
I have played around and implemented JQuery cycle to create a carousel with images,
While I have played with parts of Qt in the past I am thinking
Using asp.net 3.5 Gridview control, visual studio 2008. I have played with all the
I have j2me application in which video is played. On the client device it
I have a requirement where i have a Video that is played using MPMediaPlayerController

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.