Arbitur
Arbitur

Reputation: 39091

Playing sound in iOS7 don't seem to work

Im having a little problem playing an audio file in iOS7. The audio file doesn't get played because there is no sound when this code is called:

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PistolShootSound" ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:1.0];
[click play];

But no sound is coming out :(

I have checked the sound on my computer and it is working, I have rechecked the "Play user interface sound effects" option in system preferences but with no use. I have tried opening an AVAudioSession like this:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];

But that didn't work either.

Is there another way to play a sound in iOS7? or doesn't the simulator play sounds anymore?

Upvotes: 5

Views: 3016

Answers (1)

user1673099
user1673099

Reputation: 3299

Try this..

@property (nonatomic,strong) AVAudioPlayer *click;

@synthesize click;

NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PistolShootSound" ofType:@"mp3"]];
click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click setVolume:1.0];
[click preparetoPlay];
[click play];

Upvotes: 20

Related Questions