How do I loop a sound for an iPhone app? - iphone

I am trying to loop my sound file and I cannot find the parameter to loop the sound file! I'm about to pull out the last standing hair on my head. It loads fine....it plays....I just can't figure out the code on how to make it loop.
Here is my code:
NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:#"theeye" ofType:#"caf"];
CFURLRef eyeBGURL = (CFURLRef ) [NSURL fileURLWithPath:eyeBGPath];
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID);
AudioServicesPlaySystemSound(eyeBackGroundID);
What am I doing wrong here?
I found this:
loops
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO.
- (BOOL)loops
Return Value
YES when the receiver restarts playback when it finishes, NO otherwise.
Availability
Available in Mac OS X v10.5 and later.
See Also
– setLoops:
Declared In
NSSound.h
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops
I'm confused on how to implement this.

This is the code that worked for me. If anyone needs help with this, let me know. Someone on the Apple Dev forum pointed me to the AVFoundation docs.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/theeye3.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Here is code to pick out a sound file based on a random number from an image array.
NSString *audioWiz = [[NSString alloc]initWithFormat:#"%d.caf", randNum];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[#"%#/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 0.1;
[audioPlayer play];

I think the answer you are looking for is here.
Short answer, use a MPMusicPlayerController.

Related

How do I get sound to play in an animation I created in xcode?

I managed to create this application that includes an animation in it. But I want to make it stand out and more fun by adding a .wav file that I downloaded from the web. I want the sound to play for the duration of the animation. Only started coding a month ago so any help would be deeply appreciated
You could use the AVAudioPlayer
just create a method and connect it to your button or animation
-(void) playLoopingMusicInApplication {
AVAudioPlayer *audioPlayer;
// set the pathForResource: to the name of the sound file, and ofType: to AAC preferrably.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"WAV"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.delegate = self;
//infinite
player.numberOfLoops = -1;
[player play];
//[player release];
}
PS - You will need to get the AVFoundation framework, and the AudioToolbox framework in your project
hope this helps!

button s sound makes button press "delay" with [buttonsound prepareToPlay];

After the app starts, and I press the start button, it s "lagg" but I used this code into viewdidload and viewdidappear too:
gombhang = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"button4" ofType:#"mp3"]];
gombha = [[AVAudioPlayer alloc] initWithContentsOfURL:gombhang error:nil];
[gombha prepareToPlay];
gombha.delegate = self;
How can I fix this lagg? If I can t fix it apple will reject it?
As I know Audioplayer takes few seconds to load the audio file .
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button4" ofType:#"wav"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; AudioServicesCreateSystemSoundID((CFURLRef) CFBridgingRetain(soundFileURL), &_MySound); AudioServicesPlaySystemSound(_MySound);
And another way: I made an empty/ muted 0.1 sec sound file, and played in viewdidload, and this method loaded the avaudioplayer before I clicked on the start button, so it doesn t lagg anymore.

Setting AVAudioPlayer volume with a button

I searched everywhere but I couldn't find an answer for my problem.
I play multiple sounds on my app and I let the user to adjust the volume with a button. So if the user wants, he can play with .5 volume. So I have this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theOrchestras play];
and to reduce the volume this:
- (IBAction) volumeReduced {
theOrchestras.volume = 0.5;
}
When the sounds is playing the volume is reduced, no problem. But when the sounds stops, and plays again, the volume always goes back to 1.
To fix this I tried to save the volume as an Integer, but for some reason the value is always 0, I couldn't make it work:
.m
int64_t * volumeSaved;
.h
- (IBAction) volumeReduced {
volumeSaved = .5;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
theOrchestras.volume = volumeSaved;
[theOrchestras play];
But the audio now always plays with 0 volume, muted.
Is there any way to do this with a button?
Thanks!
You're declaring volumeSaved as a pointer to an integer instead of an integer, but then assigning to it like it's an integer. That's one oddity.
But your real problem is that the volume property in AVAudioPlayer is a float, not a int64_t. So change the type to:
float volumeSaved;
Try storing the volume with NSNumber.

MASSIVE AVAudioPlayer Memory Leaks

I am using AVAudioPLayer to do the audio for my game. I have the following code in a method in a seperate class, which is called every time I want the 'boing' sound to play. Problem is, it leaks huge amounts of memory, to the point where the game becomes unplayable. Now i'm not releasing any of the pointers in the method, because at the time I only had one. But now I have 10 of these methods.
What is the best way to release the pointers in tis case? (Yes, I have tried releasing straight after [boing play];, this solves the leak (obviously) but the sound dosen't play so theres no point.
-(void)playBoing {
int x = (arc4random()%3)+1;
NSString *path = [NSString stringWithFormat:#"/boing_0%i.aif", x];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:path];
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
boing.delegate = self;
boing.volume = 1;
[boing play];
}
It's possible there's a better solution, but I've had similar problems where releasing right away killed my process. This is the solution I took. Again, there may be better out there, but for a quick fix, it should do the trick.
In your header file, create the following:
AVAudioPlayer *boing;
Then in -(void)playBoing, do as you did, but change
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
to
if (boing != nil) {
boing = nil;
[boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
This should ensure that there's only one instance of boing allocated at a time

How to play audio file on websites on iPhone?

I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?
NSURL *bgURL = [NSURL URLWithString:#"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
[bgPlayer play];
}
AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.
Just off the top of my head, I can think of two ways that would probably work:
1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls.
2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.
I have just written answer here.Check it and replace the url used there with yours..
you just replace
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"songname" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
with
NSString *soundFilePath = #"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
There is a proper documentation available at this link
But I think you should also check the error code you are getting by this way:
NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];
Hope this helps.