user1604196
user1604196

Reputation: 31

iOS sound playback volume not respecting volume change or mute

This is my current code

void playSound(NSString* myString) {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    NSString *string = myString;
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, 
                        (__bridge CFStringRef) string, CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

It works, but it doesn't let you change the volume on the device or mute it with the mute switch. What is the simplest way to enable these features? Thanks.

Upvotes: 3

Views: 704

Answers (1)

DrummerB
DrummerB

Reputation: 40211

Why don't you use AVFoundation? If all you want is to play simple sound effects, it would be a much better choice as it is a higher level API.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];

// Assuming audioPlayer is an ivar.
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             
else 
    [audioPlayer play];

AVAudioPlayer should respect the volume settings of the user.

Upvotes: 0

Related Questions