iOS:静音切换

时间:2014-10-04 02:46:25

标签: ios objective-c audio

我是Objective-c的新手所以请轻松一点。我有一个游戏,当屏幕被点击时和发生碰撞时会播放声音。我想创建一个设置菜单,禁用播放声音。

game.h

SystemSoundID PlaySoundID;
SystemSoundID CrashSound;

game.m

- (void)viewDidLoad 
{
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"move" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);

NSURL *CrashSoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hit" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)CrashSoundURL, &CrashSound);
}

声音实现示例

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
AudioServicesPlaySystemSound(PlaySoundID);
}

在我尝试的settings.m中:

#import "GameViewController.h"
#import <AudioToolbox/AudioToolbox.h>

-(IBAction)muteButton:(id)sender {
[CrashSound stop];
}

但它说的是use of an undeclared identifier 'CrashSound'

1 个答案:

答案 0 :(得分:0)

根据Multimedia Programming Guide

  

在当前系统音量下播放声音,没有可用的编程音量控制。

也许最好使用一个可以控制每个声源音量的声音框架。这样,您就不必乱用设备的音量设置(如果它甚至可能 - 我怀疑它,因为使用将控制系统音量),因为用户可能希望设备的声音可用​​于警报或电话铃声。

我建议您使用this tutorial posted by 71 squared创建基于OpenAL的单声道声音管理器类,以便您可以为您的游戏提供按需声音,并且能够控制其音量。