Playing continuos sound in iPhone app - iphone

I have an iPhone application which requires me to play a looping mp3 sound during the entire lifetime of app.
But based on some actions performed by user this mp3 should stop/pause for while & another mp3 should play & then this mp3 should resume playing again.
I havent really used any audio API on iPhone yet, I've just used AudioToolbox for playing sounds on button taps thats all.
So what do you guys recommend I should do.
Any pointers...? suggestions....?

just as I wrote in this post, you can use AVAudioPlayer
code extract:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"mySound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL URLForString:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL];
player.numberOfLoops = -1; //infinite
[player play];
and you can pause it with:
[player pause];

Related

What Is the Easiest way to Play Sound on the Iphone?

What Is the Easiest way to Play Sound on the Iphone? I have an mp3 file, I'd rather keep it and not convert it to other format.
The simplest way I know of to play an MP3 file is to use the AVAudioPlayer class. Basically, just do (skipping error checking, setting delegate for detecting completion, etc):
NSURL* soundUrl = [[NSBundle mainBundle] URLForResource:#"soundFile" withExtension:#"mp3"];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&err];
[player play];
First add AVFoundation Framework to the project [Goto Target> Right click on project>Build Phases>Link Binary with Library> Click on +> select AVFoundation> Add]
// get file path
NSString *soundPath = [[NSBundle mainBundle] pathForResource: fileNameToPlay ofType:#"mp3"];
// allocate and refer to file
AVAudioPlayer *player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error: NULL];
// set delegate
player. delegate = self;
// play
[player play];
Apparently this turned out to be quite a mess, but I found a solution. I'm running XCode 4.4.1.
Here are the steps that worked for me:
Drag mp3 file from finder into XCode project -> Supporting Files. Make sure to copy the file. let's assume the file name is sound.mp3.
Add AVFoundation framework to your project.
import < AVFoundation/AVFoundation.h>
#property (strong) AVAudioPlayer* soundPlayer;
use the following code to init the player and play the mp3:
NSURL *chemin = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/sound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:chemin error:&error];
[self.soundPlayer play];
Now at this point, I got an exception which is described here: AVFoundation iOS 5
In short it means that there is probably a bug in the simulator which shouldn't happen on the device. In my case I turned all exception on, which stopped my code from executing. What you should do is to remove the option of throwing all exceptions.
In addition, in my case it's 3-5 seconds until it first play the sound, so be patient when you don't get the sound right away. After it play for the first time, it will play instantly.

AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting

I am trying to create buttons that play single sound files and one button that stops all of the sounds that are currently playing. If the user clicks multiple buttons or the same button in a short amount of time, the app should be playing all of the sounds simultaneously. I have accomplished this without much difficulty using the System Sound Services of iOS. However, the System Sound Services play the sounds through the volume that the iPhone's ringer is set to. I am now trying to use the AVAudioPlayer so that users can play the sounds through the media volume. Here is the code that I am currently (yet unsuccessfully) using the play the sounds:
-(IBAction)playSound:(id)sender
{
AVAudioPlayer *audioPlayer;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"Hello" ofType:#"wav"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
Whenever I run this code in the iPhone Simulator, it does not play the sound but does display a ton of output. When I run this on my iPhone, the sound simply does not play. After doing some research and testing, I found that the audioPlayer variable is being released by Automatic Reference Counting. Also, this code works when the audioPlayer variable is defined as an instance variable and a property in my interface file, but it does not allow me to play multiple sounds at once.
First thing's first: How can I play an infinite number of sounds at once using the AVAudioPlayer and sticking with Automatic Reference Counting? Also: When these sounds are playing, how can I implement a second IBAction method to stop playing all of them?
First off, put the declaration and alloc/init of audioplayer on the same line. Also, you can only play one sound per AVAudioPlayer BUT you can make as many as you want simultaneously. And then to stop all of the sounds, maybe use a NSMutableArray, add all of the players to it, and then iterate though and [audioplayer stop];
//Add this to the top of your file
NSMutableArray *soundsArray;
//Add this to viewDidLoad
soundsArray = [NSMutableArray new]
//Add this to your stop method
for (AVAudioPlayer *a in soundsArray) [a stop];
//Modified playSound method
-(IBAction)playSound:(id)sender {
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"Hello" ofType:#"wav"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
[soundsArray addObject:audioPlayer];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
That should do what you need.

Unable to play audio on device (iOS)

I am using the following code to play a audio in iOS using **AVAudioPlayer** class.
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"myAudio" ofType:#"mp3"];
NSURL *audioURL = [[NSURL alloc] initFileURLWithPath:audioPath];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
This is working fine in simulator. But, i am not getting the sound output when i run it on the device. The delegate methods ofAVAudioPlayerDelegate are also being called but there is no volume output. Where am i going wrong here.... Another important aspect i'v noticed here is that, i am getting a large message in my GDB while playing the audio file on simulator. But the same thing is not happening on device... Please refer the screenshot below for GDB messages while running on the simulator.
Check if your device is on loudspeaker mode or not..
to listen the sound through Loundspeaker use this -
// set audio to loudSpeaker mode for iphone
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
or you can increase the volume of your audio file using this -
Player.volume = 10.0;
Have you tried the .caf or .wav file? might possible that .mp3 is not working on iPhone. Also check the duration of your file not greater than 30 sec.
I was so so stupid.... My volume button was turned off. That was the reason.
Really sorry for wasting your time guys... My sincere apologies.

Best way for looping and using background sounds in iphone?

Lets say I am dragging my finger on screen and I have 1 second .caf sound file in my bundle.
So what is the best way to play my sound file in a loop till i am dragging my fingers. And it should stop whenever I remove touches.
I know touches implementation. Just post your views about using sound file.
See AVAudioPlayer class, it worked pretty well for me for similar behaviour described in your question.
This is how I did it. At the moment, I don't know of a more efficient method...but here's what I used to loop my background sound. Hope this helps.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/yourAudio.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

AVAudioPlayer only plays in simulator but not device why?! (iPhone-SDK)

I have the following simple code that plays a sound
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"wav"];
player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error:nil];
player.volume = 1;
player.numberOfLoops = 0;
[player prepareToPlay];
[player play];
For some reason this code only plays in the simulator but not the device, any ideas?
Update: Actually I installed AvTouch, a sample app from Apple onto 3.0 and that doens't work either. Is something wrong with AVAudioPlayer?
This can happen when the codec used by the audio file is available on OS X, but not the iPhone. It is also caused when the extension doesn't match the actual content of the file. Step through with the debugger and watch the debug console for codec errors or reconvert your file to a supported format with afconvert.