How to get a current volume of iPhone and change it while playing music using swift?











up vote
0
down vote

favorite












In my app I need to change volume with slider, I've got current volume with



@IBOutlet weak var volumeChange: UISlider! {
didSet {
let audioSession = AVAudioSession.sharedInstance()
let volume : Float = audioSession.outputVolume
volumeChange.setValue(volume, animated: true)
}
}


How can I change it? Now I have this method



@IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}


This method doesn't work in this case. Is it possible to make it like for example in Music app: changing volume in app with changing volume on device?










share|improve this question
























  • Give a check here into ios volume control
    – Andrew21111
    Nov 10 at 15:11










  • Why not use MPVolumeView?
    – matt
    Nov 10 at 20:22















up vote
0
down vote

favorite












In my app I need to change volume with slider, I've got current volume with



@IBOutlet weak var volumeChange: UISlider! {
didSet {
let audioSession = AVAudioSession.sharedInstance()
let volume : Float = audioSession.outputVolume
volumeChange.setValue(volume, animated: true)
}
}


How can I change it? Now I have this method



@IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}


This method doesn't work in this case. Is it possible to make it like for example in Music app: changing volume in app with changing volume on device?










share|improve this question
























  • Give a check here into ios volume control
    – Andrew21111
    Nov 10 at 15:11










  • Why not use MPVolumeView?
    – matt
    Nov 10 at 20:22













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In my app I need to change volume with slider, I've got current volume with



@IBOutlet weak var volumeChange: UISlider! {
didSet {
let audioSession = AVAudioSession.sharedInstance()
let volume : Float = audioSession.outputVolume
volumeChange.setValue(volume, animated: true)
}
}


How can I change it? Now I have this method



@IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}


This method doesn't work in this case. Is it possible to make it like for example in Music app: changing volume in app with changing volume on device?










share|improve this question















In my app I need to change volume with slider, I've got current volume with



@IBOutlet weak var volumeChange: UISlider! {
didSet {
let audioSession = AVAudioSession.sharedInstance()
let volume : Float = audioSession.outputVolume
volumeChange.setValue(volume, animated: true)
}
}


How can I change it? Now I have this method



@IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}


This method doesn't work in this case. Is it possible to make it like for example in Music app: changing volume in app with changing volume on device?







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:28









marcus.ramsden

2,1731727




2,1731727










asked Nov 10 at 14:56









Stanislav Putilov

73




73












  • Give a check here into ios volume control
    – Andrew21111
    Nov 10 at 15:11










  • Why not use MPVolumeView?
    – matt
    Nov 10 at 20:22


















  • Give a check here into ios volume control
    – Andrew21111
    Nov 10 at 15:11










  • Why not use MPVolumeView?
    – matt
    Nov 10 at 20:22
















Give a check here into ios volume control
– Andrew21111
Nov 10 at 15:11




Give a check here into ios volume control
– Andrew21111
Nov 10 at 15:11












Why not use MPVolumeView?
– matt
Nov 10 at 20:22




Why not use MPVolumeView?
– matt
Nov 10 at 20:22












2 Answers
2






active

oldest

votes

















up vote
2
down vote













To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.



In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.



It is very simple to use:



let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)


You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.



Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.



If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:



A view controller story board with a placeholder view



Once you've added that you will want something like the following in your view controller:



@IBOutlet var volumeSliderContainer: UIView!

private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()

override func viewDidLoad() {
super.viewDidLoad()

volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}


Now you will be able to interact with the MPVolumeView as you require.



Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.






share|improve this answer























  • Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
    – Stanislav Putilov
    Nov 10 at 18:10












  • I've added an example of how you might go about adding it.
    – marcus.ramsden
    Nov 10 at 19:46










  • Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
    – Stanislav Putilov
    Nov 12 at 7:10


















up vote
0
down vote













get current system volume -



var session = AVAudioSession.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()

if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}





share|improve this answer





















  • Thanx flowGlen for your answer!
    – Stanislav Putilov
    Nov 12 at 7:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240151%2fhow-to-get-a-current-volume-of-iphone-and-change-it-while-playing-music-using-sw%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.



In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.



It is very simple to use:



let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)


You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.



Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.



If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:



A view controller story board with a placeholder view



Once you've added that you will want something like the following in your view controller:



@IBOutlet var volumeSliderContainer: UIView!

private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()

override func viewDidLoad() {
super.viewDidLoad()

volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}


Now you will be able to interact with the MPVolumeView as you require.



Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.






share|improve this answer























  • Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
    – Stanislav Putilov
    Nov 10 at 18:10












  • I've added an example of how you might go about adding it.
    – marcus.ramsden
    Nov 10 at 19:46










  • Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
    – Stanislav Putilov
    Nov 12 at 7:10















up vote
2
down vote













To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.



In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.



It is very simple to use:



let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)


You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.



Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.



If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:



A view controller story board with a placeholder view



Once you've added that you will want something like the following in your view controller:



@IBOutlet var volumeSliderContainer: UIView!

private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()

override func viewDidLoad() {
super.viewDidLoad()

volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}


Now you will be able to interact with the MPVolumeView as you require.



Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.






share|improve this answer























  • Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
    – Stanislav Putilov
    Nov 10 at 18:10












  • I've added an example of how you might go about adding it.
    – marcus.ramsden
    Nov 10 at 19:46










  • Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
    – Stanislav Putilov
    Nov 12 at 7:10













up vote
2
down vote










up vote
2
down vote









To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.



In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.



It is very simple to use:



let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)


You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.



Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.



If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:



A view controller story board with a placeholder view



Once you've added that you will want something like the following in your view controller:



@IBOutlet var volumeSliderContainer: UIView!

private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()

override func viewDidLoad() {
super.viewDidLoad()

volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}


Now you will be able to interact with the MPVolumeView as you require.



Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.






share|improve this answer














To get the volume then you will need to use AVAudioSession.sharedInstance().outputVolume as you already have used.



In order to allow the user to control the volume you will want to have a look at MPVolumeView found in the Media Player framework. This component is able to make changes to the system volume.



It is very simple to use:



let volumeView = MPVolumeView(frame: volumeViewSize)
playerView.addSubview(volumeView)


You will want to use this class instead of using your UISlider instance. There are methods supplied that let you override the look and feel of the slider.



Additionally this class exposes a control which allows the user to choose the output route (iPhone, Airpods, Homepod, or Apple TV for example). You can choose to disable either the slider or the output route control so this gives you a fairly broad set of options with customising your user interface.



If you are working with a storyboard then you will need to add it in code. First create an empty UIView in the view controller's view on the storyboard and attach that to your view controller:



A view controller story board with a placeholder view



Once you've added that you will want something like the following in your view controller:



@IBOutlet var volumeSliderContainer: UIView!

private lazy var volumeView: MPVolumeView = {
let volumeView = MPVolumeView(frame: volumeSliderContainer.bounds)
volumeView.showsVolumeSlider = true
volumeView.showsRouteButton = true
volumeView.translatesAutoresizingMaskIntoConstraints = false
return volumeView
}()

override func viewDidLoad() {
super.viewDidLoad()

volumeSliderContainer.addSubview(volumeView)
NSLayoutConstraint.activate([
volumeView.widthAnchor.constraint(equalTo: volumeSliderContainer.widthAnchor),
volumeView.heightAnchor.constraint(equalTo: volumeSliderContainer.heightAnchor),
volumeView.centerXAnchor.constraint(equalTo: volumeSliderContainer.centerXAnchor),
volumeView.centerYAnchor.constraint(equalTo: volumeSliderContainer.centerYAnchor),
])
}


Now you will be able to interact with the MPVolumeView as you require.



Just watch out on the simulator, the component will not render the slider and you will only be able to see it when running your app on an actual device.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered Nov 10 at 15:14









marcus.ramsden

2,1731727




2,1731727












  • Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
    – Stanislav Putilov
    Nov 10 at 18:10












  • I've added an example of how you might go about adding it.
    – marcus.ramsden
    Nov 10 at 19:46










  • Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
    – Stanislav Putilov
    Nov 12 at 7:10


















  • Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
    – Stanislav Putilov
    Nov 10 at 18:10












  • I've added an example of how you might go about adding it.
    – marcus.ramsden
    Nov 10 at 19:46










  • Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
    – Stanislav Putilov
    Nov 12 at 7:10
















Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
– Stanislav Putilov
Nov 10 at 18:10






Thanx, Marcus. You said to use your method instead UISlider. Should I create a new swift file (class) and init it to my Slider in Storyboard? Maybe it's stoopid question, but I'm a beginner
– Stanislav Putilov
Nov 10 at 18:10














I've added an example of how you might go about adding it.
– marcus.ramsden
Nov 10 at 19:46




I've added an example of how you might go about adding it.
– marcus.ramsden
Nov 10 at 19:46












Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
– Stanislav Putilov
Nov 12 at 7:10




Thank you, Marcus! This is the most complete answer I've ever received. Everything works perfect!
– Stanislav Putilov
Nov 12 at 7:10












up vote
0
down vote













get current system volume -



var session = AVAudioSession.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()

if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}





share|improve this answer





















  • Thanx flowGlen for your answer!
    – Stanislav Putilov
    Nov 12 at 7:11















up vote
0
down vote













get current system volume -



var session = AVAudioSession.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()

if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}





share|improve this answer





















  • Thanx flowGlen for your answer!
    – Stanislav Putilov
    Nov 12 at 7:11













up vote
0
down vote










up vote
0
down vote









get current system volume -



var session = AVAudioSession.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()

if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}





share|improve this answer












get current system volume -



var session = AVAudioSession.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()

if ((try? session.setActive(true)) != nil) {
volumeChange.setValue(session.outputVolume, animated: true)
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 2:18









flowGlen

655




655












  • Thanx flowGlen for your answer!
    – Stanislav Putilov
    Nov 12 at 7:11


















  • Thanx flowGlen for your answer!
    – Stanislav Putilov
    Nov 12 at 7:11
















Thanx flowGlen for your answer!
– Stanislav Putilov
Nov 12 at 7:11




Thanx flowGlen for your answer!
– Stanislav Putilov
Nov 12 at 7:11


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240151%2fhow-to-get-a-current-volume-of-iphone-and-change-it-while-playing-music-using-sw%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."