objc-obsessive
objc-obsessive

Reputation: 825

AVAudioPlayer too loud

I've implemented AVAudioPlayer into my iPhone app but the audio being played is far too loud. To quieten the sound slightly, I changed the volume to 0.2f to see if it would have any effect. The volume stayed exactly the same. Here's the code I'm using:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Blip.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer setVolume:0.2f];

if (audioPlayer == nil)
    NSLog(@"Whoopsies, there was an error with the sound!");
else
    [audioPlayer play];

Why won't this turn the volume down?

Can somebody explain how I can?

Thanks!

Upvotes: 0

Views: 1101

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130222

Replace your code:

[audioPlayer setVolume:0.2f];

with following code:

    audioPlayer.volume = 0.2;

Hope this helps you.

Upvotes: 1

dom
dom

Reputation: 11982

As explained in Apples Audio & Video Coding How-To's: You can't set the hardware volume.

How do I access the hardware volume controller?
Global system volume, including your application's volume, is handled by iPhone OS and is not accessible by applications.

The volume of an AVAudioPlayer instance is a relative volume. You can play two sounds with two AVAudioPlayer instances and set their volume relative to each other.

EDIT:

If you want to control the global volume of your device, you can use MPVolumeView:

MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview:myVolumeView];
[myVolumeView release];

Upvotes: 0

Related Questions