How to do unlimited repetition by using AVAudioPlayer in ios - iphone

In my app i am using audio. I want to play audio for unlimited time on tapping button. Is it possible?

AVAudioPlayer has a numberOfLoops method. Set that to -1 for unlimited repetition.

Easiest way is to re-play the player when it reaches the end :
-(void)startPlayer {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"]];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.delegate = self;
if (error) {
NSLog(#"Error in audioPlayer: %#", [error localizedDescription]);
} else {
[aPlayer play];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag {
aPlayer.currentTime = 0;
[aPlayer play];
}
Call [self startPlayer]; once, and it will loop forever !

Related

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

I am getting an error when using AVAudioPlayer in Iphone. i got this error sometime not every time. error is :
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Code which i am using for playing a file :
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"abc" ofType:#"m4a"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
if(theAudio == nil)
{
NSLog([error description]);
}
else
{
[theAudio play];
}
can anyone tell me how can i solve this issue.

Can I stream a .mp3-link with the AV Foundation Framework on iOS?

I'm making an app for someone who has his own radio.
It's only a stream URL (ex. http://stream.domain.com/highquality.mp3)
I've made yet the code with the AV Foundation Framework to let it work with a simple .mp3-file.
This is my code:
-(IBAction)play {
if(clicked == 0) {
clicked = 1;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/test.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}
else {
[audioPlayer stop];
clicked = 0;
[start setTitle:#"Start" forState:UIControlStateNormal];
}
}
What should I edit?
Edit: The code above really works. I need to let it work with a button to play and stop it. I don't want to have a hole media player on my device. Simply and beautiful..
Edit 2: Some people don't find what I need: I need the edits to let it work with the .mp3 stream (as example a stream above).
You just have to pass a NSURL to your player object to make it play the stream from there
-(IBAction)play {
if(clicked == 0) {
clicked = 1;
NSURL *url = [NSURL URLWithString:#"http://www.linktoyourstream.com"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}

How to play a music file using URL link in iPhone?

I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct me?
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = #"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
This should work:
NSURL *url = [NSURL URLWithString:#"<#Live stream URL#>];
// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
//(optional) [playerItem addObserver:self forKeyPath:#"status" options:0 context:&ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:#"status" options:0 context:&PlayerStatusContext];
Use the following code to play the music:
[self.player play];
Try the following:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = #"your url"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
Try my code:
NSURL *filepath = [NSURL fileURLWithPath:audioFilePath];
[yourMediaPlayer setContentURL:filepath];
//set player.
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
^-^,I use MPMoviePlayerController with above code.
Current Answer:
Now consider Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43 link. its solution is avplayer link
Earlier Answer
//You have to pass NSURL not NSData
NSString* resourcePath = #"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSURL *url = [NSURL alloc]initWithString:resourcePath];
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
I guess you are sending
audioPlayer.numberOfLoops = -1;
to receive NSInvalidArgumentException change it to
audioPlayer.numberOfLoops = 1;
and check apple's documentation here

how to stop sound on pressing a button

I am Using Following code to play sound
NSString *laughSoundPath = [[NSBundle mainBundle]pathForResource:#"laugh" ofType:#"wav"];
NSURL *laughURL = [[NSURL alloc]initFileURLWithPath:laughSoundPath];
laughTone = [[AVAudioPlayer alloc]initWithContentsOfURL:laughURL error:nil];
[laughTone prepareToPlay];
[laughTone setDelegate:self];
[laughURL release];
-(IBAction)play
{
[laughTone play];
}
i am Using following line to stop sound
-(IBAction)stop
{
[laughTone stop];
}
Problem is.. When i play sound again, its starting from the point where it was stopped.. i mean to say its working as pause.. help me out
-(IBAction)stop
{
[laughTone stop];
[laughTone playAtTime:0];
}
Try resetting the time and then playing, again put the code in your play method:
laughTone.currentTime = 0;
[laughTone play];
Try this:
NSString *Str = [[NSBundle mainBundle] pathForResource:#"Ahhh" ofType:#"wav"];
AVAudioPlayer *audioSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL];
-(IBAction)btnStopsound(id)sender
{
[audioSound stop];
audioSound.currentTime=0;
[audioSound play]; //restart the player
}

How to stop the sound - Cocoa Touch

I have a sound looped, its a rect button, once the user presses the button the sound loops and plays. I want it so once the user has pressed the same button again, the sound stops playing.
This is the code
- (IBAction)threeSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}
Thanks
Pretty straight forward...
- (IBAction)threeSound:(id)sender; {
if (threeAudio && threeAudio.playing) {
[threeAudio stop];
[threeAudio release];
threeAudio = nil;
return;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}
You should separate those two processes
into this
`- (void)initSound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio prepareToPlay];
[threeAudio play];
}
-(IBAction)threeSound:(id)sender:(UIButton *)sender{
if (sender.selected) {
[threeAudio pause];
} else {
threeAudio.currentTime = 0;
[threeAudio play];
}
sender.selected = !sender.selected;
}`
Note that initSound needs to be called at some point, if you want to start the sound with the button only.. then add
if (!threeAudio) {
[self initSound];
}
at the beggining of the IBAction
Thats my advice ;)