Swift: how to set the iphone volume programmatically [duplicate] - iphone

This question already has answers here:
How to change device Volume on iOS - not music volume
(9 answers)
How to change an iOS device volume programmatically?
(4 answers)
Closed 6 years ago.
I am trying to create an alarm app and I have noticed that apps like Sleep Cycle will set your volume to max right before the alarm goes off, even if you had the volume at very low.
I have tried using AVAudioPlayer and all I have seen with MPVolumeView is that the user must do it.
Is there any way I can turn it up without the user having to do it?
Thank you for your help.
If I could please get the code in swift

You just need to import MediaPlayer. You can do as follow:
import MediaPlayer
And to set the volume to maximum
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(1, animated: false)

Related

How to check the iPads sound level [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get current system volume level on iPhone
I am working on an application, which includes sound. During runtime I would like to check the sound level to be able to give the user feedback by showing a pop-up with the text "the volume is too low" if the sound level is too low (just an example). I know how to check if the sound is muted or not, but how to I check the sound level?
use https://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMusicPlayerController_ClassReference/Reference/Reference.html
MPMusicPlayerController myVolumeTester = [MPMusicPlayerController iPodMusicPlayer];
float volume = myVolumeTester.volume;
This gives you a value between 0.0 and 1.0 so you can then do a check
if(volume < 0.2) {
// display "volume is too low"
}
if(volume > 0.9) {
// watch your ears!
}

Controlling the Sound of Audio

I am playing the sound in my App and sound still continue play even when App enter into background i want to control the Volume of that audio when it enter into background . Like in this APP example.I want audio volume remain maximum even user try to turn off the volume. I am unable to splve it can any one out there help me out.
if you are using AVAudioPlayer the just use AVAudioPlayer instance say player like this:
player.volume=1; //for maximum volume
This is AVAudioPlayer's volume, and in reply to your question that even if user minimizes volume of Iphone it should remain in full, then you should know that according to apple policies, there are things which have control for user only, this is what they call user privilege, your app can not control iphone volume,
and for controlling player volume (you don't need any effort if you talk about iphone volume) , i am little doubtful how you want to do this, i mean you must have some button or horizontal slider to achieve this, anyhow proceed with your logic with the delegate method
- (void)applicationDidEnterBackground:(UIApplication *)application
in your appdelegate class

iphone audio speed control programmatically [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
AVAudioPlayer: How to Change the Playback Speed of Audio?
I want to control the speed of the audio played by AVAudioPlayer, So is it possible to control the speed of the audio from slider controller?
I got solution with the help of OpenAL only. With the help of SimpleAudioEngine n CDAudioManager I have done it. referance link : http://www.learn-cocos2d.com/api-ref/1.0/CocosDenshion/html/interface_simple_audio_engine.html

How to play two mp3 audio? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
2 mp3 sounds at the same time in iphone?
Hi i am creating test application where i am using two mp3 audio file.One for the background music and second file play when user clicked the button.I am using AVAudioPlayer for both and the background music is playing in appDelegate class but i am handling it from the ViewController class.Please if anybody know about this problem please help me out.
You cannot play two compressed formats simultaneously. Try converting one of the files to AIFF, CAF or some other uncompressed format.

Volume control in MPMoviePlayerController

I am trying to figure out how to adjust the volume of a movie played with MPMoviePlayerController on the iPhone without using the slider or the volume buttons on the side of the phone. Is it even possible to do this with code in a Apple approved way? I haven't been able to find anything in the APIs that relate to changing or setting the volume during movie playback.
I know a similar question was asked a little over a year ago (Is there a way to adjust volume in MPMoviePlayer Controller by program?), but I wanted to know if anyone had found a way to change the volume in MPMoviePlayerController since then.