Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS maximum volume level for AVAudioPlayer?

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

like image 513
MartinHappyCoding Avatar asked Feb 14 '12 15:02

MartinHappyCoding


2 Answers

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.

like image 120
Peter Sarnowski Avatar answered Nov 16 '22 21:11

Peter Sarnowski


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
like image 33
Tommy Devoy Avatar answered Nov 16 '22 20:11

Tommy Devoy