问题
i try to off a sound effect on my app
play method is ' [myMusic play];
-(void)viewDidLoad {
BOOL soundIsOff = [defaults boolForKey:@"sound_off"];
//the problem is here
//xcode compiler doesn't copile this code
[myMusic play] = soundIsOff;
}
sound code : ' ///sound effect
NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL];
myMusic.delegate = self;
myMusic.numberOfLoops = 0;
Shake API Code :
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake)
//Play sound :
[myMusic play] ;
}
}
回答1:
Looks like you are trying to assign a value to a method call. That is why you are getting the compiler error:
[myMusic play] = soundIsOff //This does not work.
You probably want something like this:
if(!soundIsOff) {
[myMusic play];
}
The question is unclear however, so this is a guess at this point.
回答2:
I am confused by the question, but if what you want is to stop the sound, then you just need:
[myMusic stop];
回答3:
Like the others, I can only guess at what you want to do here. Perhaps you are trying to do this:
soundIsOff ? [music stop] : [music play];
来源:https://stackoverflow.com/questions/2554905/off-a-sound-effect-with-nsuserdefaults