Question

I have an AVAudioPlayer set to loop indefinitely in the ViewController.m. My question is how would I go about muting the AVAudioPlayer from an SKScene

//ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Space theme2"  ofType:@"mp3"]];
    _music = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_music prepareToPlay];
    [_music setVolume:0.5];
    _music.numberOfLoops = -1;
    [_music play];

so the AVAudioPlayer is playing in ViewController.m and needs to be muted/unmuted in SKScene.

//SKScene.m

if ([node.name isEqualToString:@"Mute"]) {
    //Mute Music "[_music setVolume:0.0]; isn't doing anything"
    self.Mute.position = CGPointMake(-100, self.Mute.position.y);
    self.Unmute.position = CGPointMake(160, self.Unmute.position.y);
}
if ([node.name isEqualToString:@"Unmute"]) {
    //Unmute Music [_music setVolume:0.5]; isn't doing anything"
    self.Mute.position = CGPointMake(160, self.Mute.position.y);
    self.Unmute.position = CGPointMake(-100, self.Unmute.position.y);
}

any help is appreciated

Was it helpful?

Solution

By using NSNotificationCenter:

//ViewController.m

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(stopMusic:)
                                                 name:@"StopMusic"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playMusic:)
                                                 name:@"PlayMusic"
                                               object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Space theme2"  ofType:@"mp3"]];
    _music = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_music prepareToPlay];
    [_music setVolume:0.5];
    _music.numberOfLoops = -1;
    [_music play];
}

- (void)stopMusic:(NSNotification*)notification {
    [_music stop];
}

- (void)playMusic:(NSNotification*)notification {
    [_music play];
}

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

.

// Scene.m:

if ([node.name isEqualToString:@"Mute"]) {
    [[NSNotificationCenter defaultCenter]
         postNotificationName:@"StopMusic" object:self];
    self.Mute.position = CGPointMake(-100, self.Mute.position.y);
    self.Unmute.position = CGPointMake(160, self.Unmute.position.y);
}
if ([node.name isEqualToString:@"Unmute"]) {
    [[NSNotificationCenter defaultCenter]
         postNotificationName:@"PlayMusic" object:self];
    self.Mute.position = CGPointMake(160, self.Mute.position.y);
    self.Unmute.position = CGPointMake(-100, self.Unmute.position.y);
}

OTHER TIPS

You can make use of NSNotification or delegate methods. You can check them up here and here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top