0

I need to play a sound when an action takes place, I already have the callback when the action ends and it's working fine and now I want to add a sound to it. I've added AudioToolbox as framework and this is the code I tried to use:

SystemSoundID playSoundID;
NSURL *clockSound = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"clock" ofType:@"mp3"]];  //the path for my resource
AudioServicesCreateSystemSoundID((__bridge CFURLRef)clockSound, &playSoundID);
AudioServicesPlaySystemSound(playSoundID);

I don't get any errors but the sound isn't played

Michal
  • 15,429
  • 10
  • 73
  • 104
LS_
  • 6,763
  • 9
  • 52
  • 88
  • This looks pretty similar to your question http://stackoverflow.com/questions/11525942/play-audio-ios-objective-c – The Senator Apr 17 '15 at 15:33
  • Objective-C is a language and can't play a sound by itself, so please refer to Foundation or AVAudio or whatever Apple framework you are using. – idmean Apr 17 '15 at 15:41
  • @idmean I believe at this moment of having Swift *and* Objective-C, it's valid to tag the questions with the used language. It may not be question regarding the language itself, but IMHO it helps filtering. – Michal Apr 17 '15 at 16:58
  • @Michal I was only referring to the title. I'm fine with the tag. – idmean Apr 17 '15 at 16:59

2 Answers2

3

AudioServicesPlaySystemSound() function cannot play any MP3 files. The function supports only these file formats that are .caf, .aac, or .wav and the sound must be 30 seconds or less.

Sound files that you play using this function must be: No longer than 30 seconds in duration In linear PCM or IMA4 (IMA/ADPCM) format Packaged in a .caf, .aif, or .wav file

https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html#//apple_ref/c/func/AudioServicesPlaySystemSound

So if the clock.mp3 is less than 30 seconds in duration, you can play the sound if convert the file format.

To convert mp3 to caf, for example using afconvert command, like below:

$ afconvert -f caff -d ima4 clock.mp3 clock.caf

If not (the sound is more than 30 seconds), use AVPlayer of AVFoundation.framework instead. See the link of The Senator's comment.

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL  error:nil];
[player play];
kishikawa katsumi
  • 10,418
  • 1
  • 41
  • 53
0

Try this code :

NSString *voice=[[NSUserDefaults standardUserDefaults] objectForKey:TB_Voice_conversation_value];

NSError *error;
BOOL Errors;

AVAudioSession *session = [AVAudioSession sharedInstance];
Errors = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
Errors = [session  overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
[session setActive:YES error:nil];

NSString *path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],voice];
NSURL *soundUrl = [[NSURL alloc] initFileURLWithPath:path];

_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
if (!Errors)
{
    NSLog(@"Error:%@",error);
}
NSLog(@"session : %@",session);
[_audioPlayer play];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21