9

I use the AVAudioPlayer to play a .caf file. My problem is: it is not loud enough for my purpose. Since the ringer sound of the IPhone is much louder I was wondering how I can archive this volume. I already tried to manipulate the .caf file but with no real success. Any ideas?

The code I use so far:

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 0.8];
[appSoundPlayer setDelegate: self];
    [appSoundPlayer play];

Edit:

[appSoundPlayer setVolume: 1.0];

Thx Martin

0

2 Answers 2

15

You could always set the volume to actual maximum eg:

[appSoundPlayer setVolume: 1.0];

Other than that, you probably will have to edit the file itself.

1
  • Thx Peter, right, 1.0 .. copy and paste error. But thats still too quiet ;( Feb 14, 2012 at 15:14
8

Martin, have you ensured the system volume is to a maximum? If not, you want to use the MPMusicPlayerController to turn up the system volume to the max, and not just the avaudioplayer maximum. You can turn it up for the length of your sound clip and then set it back to the original volume the user had it set at.

// Import headers. Link to "MediaPlayer Framework"
#import <MediaPlayer/MediaPlayer.h>

//set up audio session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert prepareToPlay];//audioAlert is an AVAudioPLayer declared in header

//and whereever you want to play the sound, call the lines below
float currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume; //grab current User volume
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];//set system vol to max
[audioAlert play];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setVolumeBackToNormal)userInfo:nil repeats:NO];

//and the setVolumeBackToNormal function calls this line below
[[MPMusicPlayerController applicationMusicPlayer]setVolume:currentVolume];//returns volume to original setting
2
  • 1
    Hey, don't forget to link with MediaPlayer framework.
    – Yeung
    Oct 10, 2013 at 9:50
  • 1
    Thanks! Useful comment could be that the volume property of MPMusicPlayerController is deprecated as of iOS 7.0 as stated here.
    – Alex
    Feb 29, 2016 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.