0

I am using the following code to get a sound on button click.And it works perfect. The problem is this sound is not getting reduced if i reduce the volume on the phone. I want to manage button click sound as per the volume button. I am not using input view.

SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(soundUrl) , &soundID);
AudioServicesPlaySystemSound(soundID);
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64

3 Answers3

1

As Duncan C suggests, try AVAudioPlayer:

  1. Import AVFoundation.framework.

  2. Replace your code snippet with:

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
    NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
    
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [player play];
    
siekfried
  • 2,964
  • 1
  • 21
  • 36
Joseph Chen
  • 1,520
  • 13
  • 21
  • 1
    i tried this,but getting an error No visible @ interface for AVAudioplayer declares the selector initwithurl. – Suraj K Thomas Jan 02 '14 at 06:47
  • 1
    I cleared this error ,but no sound is getting played.It is not initwithurl,it is initwithcontentsofurl. – Suraj K Thomas Jan 02 '14 at 07:34
  • 1
    @suraj can you please tell me how did you remove this error -No visible @ interface for AVAudioplayer declares the selector initwithurl. – Nitesh Mar 12 '14 at 07:23
  • 1
    @Nitesh check this link http://stackoverflow.com/questions/20878257/no-visible-interface-for-avaudioplayer-declares-the-selector-initwithurl – Suraj K Thomas Mar 12 '14 at 08:29
  • Sorry for the late response/correction, was offline for a while. Suraj, are you still unable to hear the sound? – Joseph Chen Mar 15 '14 at 19:02
0

What I do is to create an AVAudioPlayer for my button sound and play that. AVAudioPlayer honors the system volume.

I've created a separate IBAction method that plays the sound, and then I link my buttons to both actions. That way it's easy to turn the sound on and off simply by linking the action. If you always want sounds to play then you might want to put a call to a sound play method directly in your IBAction methods.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-1

Check Click here that might help you.

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
Abhishek
  • 1,682
  • 2
  • 17
  • 29