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

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

Related

Play audio without buffer

I am playing an audio file in my iphone app,I fetch the audio from a url.While playing it stops for a while to buffer.
How can I make playing audio without any buffer or play after loaded fully ?
-(void) playAudio2
{
NSURL *url = [NSURL URLWithString:#"http:domain/iphonetest/mp3/sol.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer setVolume:1];
[audioPlayer play];
}
Try using NSURLConnection or AFNetworkingKit to download the file (There is lots of resources show how to download a file. Just search for it).
Then in the -(void)connectionDidFinishLoading:(NSURLConnection *)connection delegate method for NSURLConnection or in the completionBlock for AFNetworkingKit, play the file like your code. But use NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file] for initializing url and use [audioPlayer prepareToPlay] before playing that.

How do I specify the time between loops in AVAudioPlayer?

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];
}

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.

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.