AudioServicesPlaySystemSound Volume?

2019-01-22 14:21发布

问题:

I have this code:

-(void)createAndPlaySoundID: (NSString*)name 
{
    NSString *path = [NSString stringWithFormat: @"%@/%@", [[NSBundle mainBundle] resourcePath], name];

    NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);

    AudioServicesPlaySystemSound(soundID);
}

I call this like so:

[self createAndPlaySoundID: @"mySound.caf"];

This works great, however I have no control over the volume. I simply want the volume to be controlled by the user using the standard physical volume buttons on their iPad.

If I put this volume down to say 1, it plays, great, but then if I whack the device volume up to max its the same volume.

Any help would be appreciated, handling this, thanks.

回答1:

I was also bit by a similar problem on this topic.

My problem was that there was a global setting that I was unaware of.

In the Settings app, under

General > Sounds > Ringer and Alerts

If 'Change with Buttons' is set to Off, then sounds using AudioServicesPlaySystemSound() will always be played at full volume (yet other sound API's such as AVAudioPlayer will respect the volume of the device).