Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change an iOS device volume programmatically?

Is there a way to change the device volume programmatically? maybe using audio session?

like image 683
DrCachetes Avatar asked Mar 14 '15 13:03

DrCachetes


People also ask

How do I adjust volume on IOS?

When you're on the phone or listening to songs, movies, or other media on iPhone, you can use the buttons on the side of your device to adjust the audio volume. Otherwise, the buttons control the volume for the ringer, alerts, and other sound effects. You can also use Siri to turn the volume up or down.


4 Answers

Hacky but works (Swift 3):

func setVolumeTo(volume: Float) {
  (MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)
}

Don't forget to import MediaPlayer

like image 136
budiDino Avatar answered Sep 28 '22 07:09

budiDino


I'm pretty sure that it is not possible to control the actual device volume (as this would also be a bit obtrusive) Controlling some media you're playing is another thing. You could however look into MPVolumeView: https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPVolumeView_Class/index.html for displaying a view for setting the volume.

The question has also been discussed here: How to change device Volume on iOS - not music volume

like image 34
Steffen D. Sommer Avatar answered Sep 28 '22 07:09

Steffen D. Sommer


Look at this:

import MediaPlayer

let volumeView = MPVolumeView()
if let view = volumeView.subviews.first as? UISlider{
    view.value = 0.1 //---0 t0 1.0---

}

Its working for me

like image 20
Abdul Yasin Avatar answered Sep 28 '22 07:09

Abdul Yasin


Here you go, this worked for me.

#import <MediaPlayer/MediaPlayer.h>
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1; // max volume
musicPlayer.volume = 0; // min volume (mute)
musicPlayer.volume = 0.0625; // 1 bar on the overlay volume display
like image 40
Liam Hardy Avatar answered Sep 28 '22 05:09

Liam Hardy