1

i am getting System volume from following code

[[NSNotificationCenter defaultCenter]
      addObserver:self
      selector:@selector(volumeChanged:)
      name:@"AVSystemController_SystemVolumeDidChangeNotification"
      object:nil];
 - (void) volumeChanged:(NSNotification *)  
 {
     float volume = [[[notification userInfo]
                     objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
                     floatValue];

     // here volume is the .....
 }

but how to set system or devices volume from xcode (Objective C)

  • This for your reference http://stackoverflow.com/questions/4859402/ios-change-device-volume . Hope you will find some helpful information. – HSG Apr 27 '16 at 15:28
  • Check my answer here http://stackoverflow.com/questions/36688123/how-to-set-app-volume-separately-from-system-volume-ios-device-volume-physical/36813028#36813028 – JLT Apr 28 '16 at 14:33

2 Answers2

1

It's not possible to programmatically set the system volume without calling private methods/using private APIs.

You would have to call _commitVolumeChange which is private API and will definitely get your app rejected.

Further reading here.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
0

Maybe this will help you (if you can get access to AVPlayer object and want to change the volume of your playing Asset), I use such code for modifying volume in my AVPlayer while playing video.

- (void)setVolume:(CGFloat)volumeToSet forPlayer:(AVPlayer *)avPlayer {
    AVURLAsset *asset = (AVURLAsset *) [[avPlayer currentItem] asset];
    NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

    NSMutableArray *allAudioParams = [NSMutableArray array];
    for (AVAssetTrack *track in audioTracks) {
        AVMutableAudioMixInputParameters *audioInputParams =    [AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:volumeToSet atTime:kCMTimeZero];
        [audioInputParams setTrackID:[track trackID]];
        [allAudioParams addObject:audioInputParams];
    }
    AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
    [audioZeroMix setInputParameters:allAudioParams];

    [[avPlayer currentItem] setAudioMix:audioZeroMix];
}

More details about this solution here Adjusting the volume of a playing AVPlayer

If you would like to change device's volume look here, this is workaround and may broke in some iOS versions, but works now in iOS 7, iOS 8, iOS 9 (tested it). iOS 9: How to change volume programmatically without showing system sound bar popup?

Community
  • 1
  • 1
Alexander Tkachenko
  • 3,221
  • 1
  • 33
  • 47