Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play a sound file at maximum volume in iOS without a sound indicator change

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?

like image 282
gabe.roze Avatar asked Feb 19 '13 16:02

gabe.roze


People also ask

How do I stop my iPhone from changing volume?

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.

How do you increase iPhone DB?

Go to Settings > Accessibility > Audio/Visual, then adjust the Balance slider. Set the audio volume balance to the center.

How do I change the max volume on IOS 14?

"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."


1 Answers

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.

like image 119
Ashley Avatar answered Sep 24 '22 03:09

Ashley