IOS sound volume

I play like this:

#import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> .. .. SystemSoundID soundID; NSString *path = [[NSBundle mainBundle] pathForResource:@"ClickSound" ofType:@"wav"]; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID); AudioServicesPlaySystemSound (soundID); 

This seems to be the volume from the β€œRinger”, but when I use the physical volume button, it controls the β€œVolume” (so I can β€œMute” the volume, but I still hear the sound).

I want to control the correct volume, and I do not want it to play when it was turned off (BTW - when I use mute toggle, it works, and I can not hear the sound).

How can i fix this?

+5
source share
3 answers

AudioServicesCreateSystemSound applies only to ringer volume.

You can use AVAudioPlayer for this. Here is a sample code:

 AVAudioPlayer *buttonClick=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"buttonClick"] ofType:@"mp3"]] error:NULL]; [buttonClick prepareToPlay]; [buttonClick play]; 
+2
source

There is no way to control the volume in the code. You can provide a user interface control so that the user can change the volume.

Audio is category driven and the behavior is different in terms of equipment parameters.

The Ambient category will mark the mute switch; others will not.

No category allows you to determine the position of the mute switch.

0
source

You should probably register for the Volume button. Callbacks using the code below,

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

Hope this helps. Happy coding :)

0
source

Source: https://habr.com/ru/post/1238766/


All Articles