off a sound effect with NSUserDefaults

半城伤御伤魂 提交于 2019-12-13 04:45:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!