How to expose class members or how to implement method acting on those members
I am trying to stop a sound from another class. As soon as the application opens the sound starts to play and is set to play on a loop unless the 'User' changes the settings and turns sound to OFF.
This works but only on start up of the application where it checks to see if the settings sound is set to 'ON/OFF' but I would like if it would do it when changed in the settings.
This is what I have so far...
FirstClass
// 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 =
[[A开发者_StackOverflow社区VAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
// set our ivar equal to the new player
self.player = newPlayer;
[newPlayer release];
// preloads buffers, gets ready to play
[player prepareToPlay];
player.numberOfLoops = -1; // Loop indefinately
if ([SoundSwitch isEqualToString:@"1"]){
[self.player play]; // Plays the sound
}else{
[self.player stop]; // Stops the sound
}
This plays the sound. and if I wanted to stop it simply:
[self.player stop]
But this only works in the same class how would I get it to work in another?
I would use NSNotifications, so that you can send stop or play notifications from anywhere in your application. This is how you do it:
in your init method for FirstClass do this:
//Notification for stoping
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stop) name:@"StopSound" object:nil];
//Notification for playing
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(play) name:@"PlaySound" object:nil];
Now create those two methods from the selectors (stop method and play method)
-(void)stop{
[self.player stop]
}
-(void)play{
[self.player play]
}
in the dealloc remember to remove the notification observers:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"StopSound" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PlaySound" object:nil];
Now from anywhere in your application just send the notification to stop or play the sound
//Stop
[[NSNotificationCenter defaultCenter] postNotificationName:@"StopSound" object:nil];
//Play
[[NSNotificationCenter defaultCenter] postNotificationName:@"PlaySound" object:nil];
I will shoot here in a dark as I don't know those apple stuff but I think you have to create instance of AVAudioPlayer
where you want to manipulate your sound from. If this class you presented is accessible in a second place you want to play/stop sound maybe you could expose your 'player' member to external use? or what could be better you can add to this class methods PlaySound(), StopSound() and by them allow external code to play sounds.
Sample code with a little help from google :)
- (void)playSound:{
[self.player play];
}
- (void)stopSound:{
[self.player stop];
}
and now you can invoke playSound
and stopSound
methods from outside of your class
精彩评论