How do I specify the time between loops in AVAudioPlayer? - iphone

I am making a metronome app, my sound file is a very brief tick sound, so I need to be able to change the time in between loops to get the right beats per minute. Here's the code to continuously play the tick sound. I would like to specify a delay in between each loop of the audioPlayer.
NSURL *url = [NSURL fileURLWithPath:
[NSString stringWithFormat:#"%#/Metronome-Sound.mp3",
[[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

Since there is no more api's in iPhone. One of the most easiest way is to use [NSThread sleepForTimeInterval:] in the delegate method -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
EDIT
Simple in .h file after class name implement delegate like this <AVAudioPlayerDelegate>
Then in .m file implement the delegate method as below
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[NSThread sleepForTimeInterval:5.0];
}

Related

Is it possible to autoplay audio and play/pause with button?

I need to play audio automatically after open the view and have one button to control play/pause event. I heard that it has a prepareToPlay to control the audio by button but it cannot play audio automatically.
Here is my code:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"track8"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer prepareToPlay];
and in the button:
-(IBAction)playPauseButtonPress:(id)sender{
if (self.isPlaying) {
[self.audioPlayer pause];
self.isPlaying = NO;
}else {
[self.audioPlayer play];
self.isPlaying = YES;
}
}
Is it possible to autoplay audio and play/pause with button? How?
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"track8"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer prepareToPlay];
There is method play is available for playing it. You just need to add following line:
[audioPlayer play];
Please check the AVAudioPlayer documentation
play >
Calling this method implicitly calls the prepareToPlay method if the audio player is not already prepared to play.
prepareToPlay > Calling this method preloads buffers and acquires the audio hardware needed for playback, which minimizes the lag between calling the play method and the start of sound output.Calling the stop method, or allowing a sound to finish playing, undoes this setup.
There is nothing like you can't pause the audio if you use play instead of prepareToPlay.
Hope this helps

Problem with AVAudioPlayer ? Creating delay

While using AVAudioPlayer .... Sound not playing at instant if i click button... and in that class i have one NSTimer which keep changing image so for that purpose i have to ring sound.
But If i will use this AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);... i am not able to hear sound in device.. cause my file is in mp3 format.
AVAudioPlayer Code:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/slotmachine.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
So anyone can tell me why i am not able to use AVAudioPlayer so smoothly like System Sound does.. what is the appropriate way to integrate ?
Call [audioPlayer prepareToPlay] well before you want to actually play the audio.

How can I play a music such that it automatically replay it after its end?

AVAudioPlayer *myExampleSound;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound play];
How can I play myExampleSound such that it automatically replay after its end?
I am using the above code.
Anyone please help.
The AVAudioPlayerDelegate has got a method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
which is called after the sound has finished playing. You have to implement this method and then you are able to play your sound again.

Download and play mp3 file from url with iphone application

We want to create an application that downloads a mp3 file and then starts to play it.
Has anyone an idea of how to download the mp3 file from an url and then store it, either on the iphone or in the application (depending on what is possible).
Add the following import statement to your header:
#import <AVFoundation/AVFoundation.h>
In your implementation somewhere:
NSError *error;
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://xyz/abc.mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.delegate = self;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(#"%#", [error description]);
else
[audioPlayer play];
Don't forget to release the player at some point:
[audioPlayer release];
dirt simple, on a thread:
myMP3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."]];
slightly less simple, but with more options:
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://..."]] delegate:self];
Or just play it directly:
[[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://..."] error:nil] play];
Edit: the above snippet would leak. You should store the created AVAudioPlayer instance in a member and release it when the audio finishes playing or you are otherwise done with it. I do not know off hand if releasing a playing AVAudioPlayer stops the audio.

playing audio files from table, iphone

I've created a list of music file names in an array which is used to populate an uitableview. I want to make the audio player take this array or table as the playlist and play them continuously one by one, with all the features like play, pause, next, previous, forward, backward and stop. What is the best way to implement this. All the music files in the resource folder itself so that i can initiate each one as
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
Could this be as simple as implementing the AVAudioPlayerDelegate?
appSoundPlayer.delegate=self;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (successfully)
{
// play next track
}
}
Obviously you'd need to keep track of which track is currently playing in order to work out the next track to play, but that shouldn't be a problem. You'd also have to implement your own controls and logic for track skip etc, but again this is easily done.