In my app I'm required to play a sound from a volume of 0 to maximum system volume (it's an alarm app).
I currently use AVAudioPlayer
to play the sound and MPMusicPlayerController
to maximize the device's volume for the duration of the alarm
MPMusicPlayerController.applicationMusicPlayer.volume = 1.0;
NSString *fileName = [NSString stringWithFormat:alarm.sound];
fileName = [fileName stringByDeletingPathExtension];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"aifc"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.volume = 0.0;
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
audioPlayer.play;
and then I schedule a timer to increase the volume
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(pollTimeForGradualVolumeIncrease)
userInfo:nil repeats:YES];
//Increase the volume gradually
- (void)pollTimeForGradualVolumeIncrease
{
timerSecond += 1;
if (audioPlayer.volume < 1.0 && timerSecond % 1 == 0)
{
[audioPlayer setVolume:audioPlayer.volume + 0.02];
UISlider *slider = volumeSliderView.subviews[0];
[slider setValue:1.0 animated:NO];
}
else if (audioPlayer.volume >= 1.0)
{
timerSecond = 0;
timer.invalidate;
timer = nil;
}
}
The problem with this is that the iOS volume indicator will appear when setting MPMusicPlayerController.applicationMusicPlayer.volume
Is there a way to play a sound file at maximum device volume without the displaying the iOS volume changed indicator?
Lock the ringer and alert volumes in SettingsGo to Settings . Tap Sounds & Haptics (on supported models) or Sounds (on other iPhone models). Turn off Change with Buttons.
Go to Settings > Accessibility > Audio/Visual, then adjust the Balance slider. Set the audio volume balance to the center.
"You can limit the maximum headphone volume for music and videos. Go to Settings . Tap Sounds & Haptics (on supported models) or Sounds (on other iPhone models). Tap Reduce Loud Sounds, turn on Reduce Loud Sounds, then drag the slider to choose the maximum decibel level for headphone audio."
I was experimenting with related things 8 months ago, and as far as I remember, if an MPVolumeView is present, iOS then doesn't show the system volume changed indicator.
If you don't actually want to see the MPVolumeView, I suppose you could hide it behind another view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With