Looping Audio with AudioToolbox.framework - ios

I have would like to play audio on the startup of my app, but, first of all, the audio doesn't play! No matter how i code it. And secondly I would love to loop it if i can.
#implementation ViewController
-(void)awakeFromNib
{
SystemSoundID soundID;
NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:#"ScaryMusic" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);
AudioServicesPlaySystemSound(soundID);
NSLog(#"Sound Played");
}

Try this instead:
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:#"ScaryMusic" withExtension:#"mp3"];
if (soundFileURL != nil)
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
}
[player play];
This is better for mp3's and such. SystemSound is mostly reserved for short .wav type sounds.

If you look in the docs you'll find that mp3 is not among the formats supported by AudioToolbox

Related

AVPlayer doesn't play anything

I am doing something incredibly simple, just play an audio.
but it doesn't play anything. not on the simulator or device.
audio file is in bundle and preview can play the audio, I have also tried different audios nothing plays.
What could be the problem?, do I have to add delegate methods for avplayer?(if so that doesn't make sense to me)
I have also tried without avplayer item and different nsurl methods too...
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"JohnsPizza" ofType:#"aiff"];
//NSURL *assetURL = [[NSBundle mainBundle] URLForResource:#"JohnsPizza" withExtension:#"aiff"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [[AVPlayer alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
I have also tried but no luck
// SystemSoundID sound1;
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1);
// AudioServicesPlaySystemSound(sound1);
// AudioServicesRemoveSystemSoundCompletion(sound1);
// AudioServicesDisposeSystemSoundID(sound1);
you gotta retain AVPlayer object as it may set to nil automatically
#property (strong, nonatomic) AVPlayer *player;

App Audio Not Playing

So in my file I have the following code with the intention that a music file plays in the background :
-(void) BackGroundMusic {
NSString *soundPath = [[NSBundle mainBundle]pathForResource:#"LostInTheSea" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}
And then in the view did load section I have :
[self BackGroundMusic];
I have all the toolboxes added such as AVFoundation and AudioToolbox but i can not hear any audio playing . Please help !
You should not be using system sound to play mp3 files as user523234 explains.
Use AVAudioPlayer instead. Here's an example that will loop infinitely:
#import "AVFoundation/AVAudioPlayer.h"
#import "AVFoundation/AVAudioSession.h"
-(void)playbgMusic {
AVAudioPlayer *soundbgMusic;
NSString *soundPath;
soundPath= [[NSBundle mainBundle] pathForResource:#"bgMusic" ofType:#"mp3"];
soundbgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
soundbgMusic.volume = 1.0;
soundbgMusic.numberOfLoops = -1;
[soundbgMusic prepareToPlay];
soundbgMusic.currentTime = 0.0;
[soundbgMusic play];
}
According to the document for AudioServicesPlaySystemSound:
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

Adding a sound effect to a jump

I want to add a sound effect to a jump action when my character jumps. It sounds simple to do but i just cannot do it, this is what I've tried so far..
In my header file for my character I have:
SystemSoundID jump;
- (void)playSound:(NSString *)jump1 :(NSString *) mp3;
In my implementation file I have:
- (void)playSound :(NSString *)jump1 :(NSString *) mp3{
SystemSoundID audioEffect;
NSString *path = [[NSBundle mainBundle] pathForResource : jump1 ofType :mp3];
if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
NSURL *pathURL = [NSURL fileURLWithPath: path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else {
NSLog(#"error , file not found aye: %#", path);
}
}
Also inside the function jump I have:
[self playSound:#"jump" :#"mp3"];
Please excuse my inexperience I just had a go at it as it would be a great feature for my game, if someone could tell me what I am doing wrong or a different and better way to do it that works that would be great thanks!
Add this method in a class and whenever you want to play the sound just call this method using [self playSound], jump.mp3 should be in your bundle, and remember one thing more don't forget to add AudioToolbox framework.
-(void) playSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"jump" ofType:#"mp3"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
}

stop first sound when second is playing

I am an absolute beginner in both objective-c and other environments so please be patient. Here is my problem: I need to play sound files so that each of them stops when the other is started, right now they overlap; here is a snippet of my code.
Thank you in advance and let me know if you need other details.
- (IBAction)soundfirst {
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player1 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player1.delegate = self;
[player1 play];
}
- (IBAction)soundsecond {
[player1 stop];
NSString *path =
[[NSBundle mainBundle] pathForResource:#"hi2" ofType:#"mp3"];
player2 = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
player2.delegate = self;
[player2 play];
}
The best way to do something when you're stuck, is to look into the documentation.
In your code, you're creating an object of type AVAudioPlayer, and naming it player1.
AVAudioPlayer Documentation:
http://developer.apple.com/library/ios/#DOCUMENTATION/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
look under the instance methods, and there's a method there called stop.
So in the first method, you can do something like
if(player2.playing){
[player2 stop];
}
[player1 play];
#import <AudioToolbox/AudioServices.h>
SystemSoundID buttonClickSoundID;
+ (void)playButtonClickSound {
if (buttonClickSoundID == 0) {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"button_click" ofType:#"wav"];
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &buttonClickSoundID);
}
AudioServicesPlaySystemSound(buttonClickSoundID);
}

AVAudioPlayer not playing sound on some devices

I am using AVAudioPlayer to play an audio file its working but on some device it does not play how do i solve my problem
if(_defaultSettings)
{
_soundFilePath = [[NSBundle mainBundle] pathForResource:#"ring" ofType:#"wav"];
NSLog(#"ringTone Path%#",_soundFilePath);
// /System/Library/Audio/UISounds/
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:_soundFilePath ]error:nil];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
_audioPlayer.numberOfLoops = -1;
AudioServicesPlaySystemSound(1000);
}
- (void)playSystemSound {
NSLog(#"playSystemSound");
// Create the URL for the source audio file. The URLForResource:withExtension: method is
// new in iOS 4.0.
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"short_low_high"
withExtension: #"wav"];
if (!self._soundFileURLRef) {
// Store the URL as a CFURLRef instance
self._soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (_soundFileURLRef, &_soundFileObject);
}
// Play the sound
AudioServicesPlaySystemSound (_soundFileObject);
// // to vibrate the device // AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

Resources