2

I have a firing sound in the iOS app I'm developing, although at the moment the volume of the sound is controlled by the ringer volume, not the main volume. I thought it was because it was a .caf file, but after replacing it with an mp3 it did not change the volume type it is controlled with.

I think it is because I'm using AudioServicesCreateSystemSoundID, which I'm assuming applies only to the ringer volume.

- (IBAction)fire_rifle {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"rifle", CFSTR ("caf"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
}

How would I change this code so that the main volume is used instead of the ringer volume?

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
Dale Townsend
  • 671
  • 3
  • 13
  • 25

3 Answers3

1

I would suggest checking out AVAudioPlayer. It works with the main volume and has other handy methods to adjust playback which you may take advantage of.

The Kraken
  • 3,158
  • 5
  • 30
  • 67
0

As stated in the other answers you can use AVAudioPlayer for this. However, here is some sample code:

<AVAudioPlayerDelegate>

Be sure to include this ^^

- (void)myMethod{
        AVAudioPlayer *data;
        NSString *name = [[NSString alloc] initWithFormat:@"mySoundName"];
        NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
        if (data) {
            [data stop];
            data = nil;
        }
        data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
        data.delegate = self;
        [data prepareToPlay];
                [data play];

}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
-1

As per this, you cannot change device volume programatically, however MPVolumeView (volume slider) is there to change device volume but only through user interaction.

AVAudioPlayer is there if you want application level volume. In this case you can use volume property to set your application volume (not device volume) programatically.

Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139