代码之家  ›  专栏  ›  技术社区  ›  Mc.Lover

使用nsuserdefaults关闭声音效果

  •  0
  • Mc.Lover  · 技术社区  · 15 年前

    我试图关闭我的应用程序上的声音效果

    播放方法为“[我的音乐播放];

    -(void)viewDidLoad {
         BOOL soundIsOff = [defaults boolForKey:@"sound_off"];
         //the problem is here 
         //xcode compiler doesn't copile this code 
         [myMusic play] = soundIsOff;
    
    }
    

    声音代码: ’ ///音效

     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代码:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    
     if (event.subtype == UIEventSubtypeMotionShake)
    
      //Play sound :
      [myMusic play] ; 
     }
    }
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   Jeff B    15 年前

    似乎您正在尝试为方法调用分配值。这就是为什么会出现编译器错误:

    [myMusic play] = soundIsOff //This does not work.

    你可能想要这样的东西:

    if(!soundIsOff) {
        [myMusic play];
    }
    

    不过,这个问题还不清楚,所以这是目前的猜测。

        2
  •  0
  •   crimson_penguin    15 年前

    我被这个问题搞糊涂了,但是如果你想要的是停止声音,那么你只需要:

    [myMusic stop];
        3
  •  0
  •   glorifiedHacker    15 年前

    和其他人一样,我只能猜测你想在这里做什么。也许你是想这么做:

    soundIsOff ? [music stop] : [music play];