2

I am making an iOS 8 Objective-C app (deployed on my iPhone 5) and I'm using this code to play a sound through the phone from the app:

@property (assign) SystemSoundID scanSoundID;

...

- (void)someFunction {

    ...

    //Play beep sound.
    NSString *scanSoundPath = [[NSBundle mainBundle]
                               pathForResource:@"beep" ofType:@"caf"];
    NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)scanSoundURL, &_scanSoundID);
    AudioServicesPlaySystemSound(self.scanSoundID);

    ...

}

This code works fine, but the beep.caf sound is quite loud. I want to play the beep sound at 50% volume (without changing the volume of the iPhone). In other words, I don't want to touch the iPhone's actual volume, I just want to play the sound with less amplitude so to speak.

How can I accomplish this (preferably with Audio Services like I'm using now)?

UPDATE
After trying to implement the answer from Louis, this code is not playing any audio (even though my phone is not on silent and my volume is turned up):

NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
                                                          ofType:@"caf"];
NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL 
                                                               error:nil];
player.volume = 0.5;
[player play];
Ethan G
  • 1,353
  • 2
  • 15
  • 31
  • hmm... try implementing the AVAudioPlayers delegate methods, specifically `audioPlayerDecodeErrorDidOccur:error` and see if you're getting an error when trying to play. Idk if it will matter, but also try sticking the call to play inside of an `[NSOperationQueue mainQueue] addOperationWithBlock{}];` – Louis Tur Jan 04 '15 at 07:02
  • nevermind.. try a breakpoint and make sure that your URL isnt nil. And check your project settings under BuildPhases and make sure that your caf file is listed under Copy Bundle Resources – Louis Tur Jan 04 '15 at 07:23

3 Answers3

3

Your UPDATE code creates player as a local variable which will go out of scope when the method (in which you call this code) returns.

As soon as player goes out of scope, it is released and the audio does not even get a chance to start playing.

You need to retain player somewhere:

@property AVAudioPlayer* player;

...

- (void)initializePlayer
{
    NSString *scanSoundPath = [[NSBundle mainBundle] pathForResource:@"beep"
                                                      ofType:@"caf"];
    NSURL *scanSoundURL = [NSURL fileURLWithPath:scanSoundPath];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:scanSoundURL 
                                                           error:nil];
    self.player.volume = 0.5;
}

Then somewhere else you'd call:

[self.player play];
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
2

From the Multimedia Programming Guide under "Playing UI Sound Effects or Invoking Vibration Using System Sound Services":

In addition, when you use the AudioServicesPlaySystemSound function:

  • Sounds play at the current system audio volume, with no programmatic volume control available

So, it doesn't appear to be possible according to the Apple Docs. But the AVAudioPlayer class lets you do it via its volume property.

In your current code, all you'd have to do is add

AVAudioPlayer *newPlayer = 
        [[AVAudioPlayer alloc] initWithContentsOfURL: scanSoundURL
                                               error: nil];
[newPlayer play];
Louis Tur
  • 1,303
  • 10
  • 16
0

Objective C

Play Sound With System Volume

@interface UIViewController () {
    AVAudioPlayer *audioPlayer;
}

-(void) PlayCoinSound {
//#import <AVFoundation/AVFoundation.h>
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"CoinSound" ofType:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:soundPath] error: nil];
    audioPlayer.volume = 1.0;
    [audioPlayer prepareToPlay];
    [audioPlayer play];
}

Play with Full Sound

- (void)PlayWithFullVolume {
    //#import <AudioToolbox/AudioToolbox.h>
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
    AudioServicesPlaySystemSound (soundID);

}
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40