I have a simple soundboard app with a few different categories of sounds. I am using the AudioToolbox.framework (all of my sound files are basically under 10 seconds and are .wav files) but I am confused on how to make my app refer to the "Volume" volume, not the "Ringer" volume.
If my device is set to "Silent," no sound plays from my buttons, even if the device volume is turned up. However, as soon as I turn on the "Ringer" (located on the side of my device) sound plays as loud as the ringer is turned up.
I searched around online and found some sources that said to switch my AVAudioSession to AVAudioSessionCategoryPlayback, so I pasted this
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
into my viewDidLoad. However, the same problem occurs. I have found other suggestions online but the explanations leave me confused about what I'm supposed to do. I'm relatively new to Objective-C and coding, so please be specific and clear in your explanations if you know the answer.
I gladly appreciate any help you can provide.
Edit One I followed the suggestion of Pau Senabre, and I did not notice any changes. The audio still didn't play while the Ringer is on silent. Current code with Pau's changes:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *sessionError = NULL;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:&sessionError];
if(!success) {
NSLog(@"Error setting category Audio Session: %@", [sessionError localizedDescription]);
}
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
Any other suggestions?
Edit Two I followed the suggestions of Daniel Storm, who suggested I set my AVAudioSession
category to Playback
in a way slightly different than before. He suggested I do
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
but this still left my volume muted while the ringer was muted.
What could I be doing wrong?
Here is how I am playing my audio:
NSURL *DariusSelectSound = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"DariusSelect" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)DariusSelectSound, &DariusSelectAudio);
When I press a button, I play the sound with the code:
AudioServicesPlaySystemSound(DariusSelectAudio);
My volume is turned up to about half way, and the ringer is at roughly the same volume. However, adjusting my regular volume has no effect, the audio gets louder or quieter by adjusting my ringer volume.
Edit Three Problem is solved and there are multiple solutions. The recommendations by Pau Senabre and Daniel Storm both work. My problem was that I was trying to use AudioServices
when I needed the AVAudioPlayer
. Sorry that I made such a simple mistake, and I thank you greatly for your help!