如何在点击newsong时停止播放前一首歌的avaudioplayer?

时间:2015-02-20 08:18:36

标签: ios objective-c iphone avaudioplayer

我用按钮歌创建了一个视图。

当我们点击按钮时,它会转到包含歌曲列表的表格。 当我们选择一首歌时,它会转到另一个视图并播放该歌曲。 每当歌曲正在播放时,我们都可以回到主视图或歌曲将在背景中播放的歌曲表格,这里很好。

但每当我尝试从歌曲表中播放另一首歌曲时,前一首歌曲就不会停止,并且会继续与所选歌曲一起播放。

我的目标是在切换到不同视图时播放歌曲。我已经完成了这个,但我想要的是每当我从歌曲表中选择另一首歌时,它必须停止上一首歌并播放所选歌曲。

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

}

}

/ ******************************************  从服务加载歌曲  ************* /

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

}


}

1 个答案:

答案 0 :(得分:2)

每次从表格加载歌曲时,您都在创建音频播放器的实例。这会创建多个实例。因此,无论何时播放歌曲,它都将在新的音频播放器实例中播放,之前的歌曲(具有不同的音频播放器实例)也将继续播放。所以我建议你应该在类级别创建音频播放器而不是每次都创建实例。这将保留单个实例并为您播放一首歌。