Surya
Surya

Reputation: 151

How to stop an avaudioplayer playing previous song while clicking on newsong?

I created a view with a button songs.

When we click on button it goes to a table which contains list of songs. When we select a song it goes to another view and plays the song. Whenever the song is playing we can go back to main view or to the table of songs the song will be playing in background its fine upto here.

But whenever I try to play another song from the table of songs the previous song is not stopped and it continues to play along with the selected song.

My aim is play the song when it is switched to a different view. I have done this one but what I want is whenever I select another song from the table of songs it must stop the previous song and play the selected song.

-(void)playOrPauseButtonPressed:(id)sender

{

[self.view addSubview:today_slokaText];

if(playing==NO)

{

[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;


[playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

[self.view addSubview:today_slokaText];

 // Here Pause.png is a image showing Pause Buttomn

NSError *err=nil;

if (!audioPlayer)

{

[self loadSongAtIndex: selectedIndex];

playing=YES;

}

audioPlayer.numberOfLoops = 0;

[audioPlayer prepareToPlay];

[audioPlayer play];

audioPlayer.delegate=self;

if(!audioPlayer.playing)

{

[audioPlayer play];

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

[self.view addSubview:self.activityIndicator];

self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);

[self.activityIndicator startAnimating];

[playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

[bg_Image addSubview:activityIndicator];

}

playing=YES;

}

else if (playing==YES)

{

[self.activityIndicator stopAnimating];

self.activityIndicator.alpha=0.0;

// [bg_Image addSubview:activityIndicator];

[playButton setBackgroundImage:[UIImage imageNamed:@"play12.png"] forState:UIControlStateNormal];

[self.view addSubview:today_slokaText];

[audioPlayer pause];

playing=NO;

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateViewForPlayerState) userInfo:nil repeats:YES];

[self.view addSubview:today_slokaText];

}

}

/****************************************** load the song from service *******************************************/

- (void)loadSongAtIndex:(NSInteger) songIndex

{

audioSession=[AVAudioSession sharedInstance];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

[[AVAudioSession sharedInstance] setActive: YES error: nil];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

urlsArray=[[NSMutableArray alloc]initWithObjects:@"http://01_ARDHANAREESWARASTOTRAM.mp3",@"http://02_AshtaLakshmiStotram.mp3",@"http://03_AYIGIRINANDININANDITHAMEDINI.mp3",, nil];

NSString *sourcePath = [urlsArray objectAtIndex:songIndex];

NSURL *audioURL=[NSURL URLWithString:sourcePath];

NSData *data=[NSData dataWithContentsOfURL:audioURL];

audioPlayer = [[AVAudioPlayer alloc] initWithData:data  error:NULL];

audioPlayer.numberOfLoops = 0;

[audioPlayer prepareToPlay];

[audioPlayer play];

audioPlayer.delegate=self;

if(!audioPlayer.playing)

{

[audioPlayer play];

}

if (self.audioPlayer)

{

[self updateViewForPlayerInfo];

[self updateViewForPlayerState];

[self.audioPlayer setDelegate:self];

}


}

Upvotes: 1

Views: 472

Answers (1)

Jasmeet Singh
Jasmeet Singh

Reputation: 564

You are creating the instance of your audio player everytime you load a song from the table. This creates multiple instances. So whenever you play a song it will be played in the new audio player instance and the previous songs(with different audio player instances) will also keep playing. So what i suggest is that you should create audio player at the class level and not create instances everytime. This will hold the single instance and will play a single song for you.

Upvotes: 2

Related Questions