0

I have executed the following code ,but sound is not working. in .h

 #import <AVFoundation/AVAudioPlayer.h>
 #import <AVFoundation/AVFoundation.h>

in .m

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl];

[player play];

But I'm unable to hear any sound.Actually i wanted to manage button click sound along with volume button,so I'm using this method.

Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64

3 Answers3

1

Did you set the volume?

[player setVolume:1.0];

Another way to play a short sound is AudioServicesCreateSystemSoundID.

See the link Play a short sound in iOS

Community
  • 1
  • 1
KudoCC
  • 6,912
  • 1
  • 24
  • 53
1

Try with :

 self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:(__bridge NSURL *)(soundUrl) error:NULL];

    self.theAudio.volume = 1.0;
    self.theAudio.delegate = self;
    [self.theAudio prepareToPlay];
    [self.theaudio play];
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
1

This works perfect for me.

Add AVFoundation and AudioToolBox frameworks into the project

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface TestViewController : UIViewController
{
    SystemSoundID SoundID;
}

@implementation TestViewController
-(void) viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"caf"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SoundID);  //When using ARC, make sure to use __bridge
 }

-(void) playSound {
     AudioServicesPlaySystemSound(SoundID);
 }