目标C语言 iOS的声音播放静音



我正在为幼儿制作一个学习字母表的应用程序,我遇到了一个声音输出的问题。在我的ipad上测试时,无论设备的声音设置为最大还是静音,应用程序播放的每个声音都具有相同的音量。换句话说,无论设备的音量设置如何,都会发出相同的声级。

我用来播放声音的代码是(以a音为例):
- (IBAction)aSpill:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"aLyd", CFSTR("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

任何帮助都非常感谢!:)

请在这里查看答案AudioServicesPlaySystemSound音量?它解释了系统声音并不总是使用系统音量。

我建议使用AVAudioPlayer来播放您的声音。

所以从这里:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

试着用下面的代码来跟随代码。

NSString *soundFilePath =
            [[NSBundle mainBundle] pathForResource: @"sound"
                                            ofType: @"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
            [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                   error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];

最新更新