2013-07-23 183 views
0

我打電話這個ViewController從另一個UITableViewController其通過歌曲名稱和index.For的第一次它播放的歌曲,但是當我回到tableView並單擊另一首歌曲也同時播放兩首歌曲。AVAudioPlayer同時播放多首歌曲?

.h文件我用AVAudioPlayer

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 

.m文件:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[self sliderValueChanged:volumeSlider]; 
[self updateTime]; 
[self playOrPauseSound:nil]; 
volumeSlider.value=0.5; 
count=songArray.count; 

[self setupAVPlayerForURL:songIndex]; 
_audioPlayer.delegate=self; 
} 


-(void) setupAVPlayerForURL: (int) url { 

NSString *songname=[NSString stringWithFormat:@"%@",songArray[songIndex]]; 
songTitle.text=songname; 
NSString* saveFileName = songname; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:saveFileName]; 
NSURL *url1 = [[NSURL alloc] initFileURLWithPath: path]; 
self.audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:NULL]; 

[self.audioPlayer play]; 
self.seekBarSlider.minimumValue = 0.0f; 
self.seekBarSlider.maximumValue = self.audioPlayer.duration; 
[self updateTime]; 

self.isPlaying=YES; 
_audioPlayer.delegate=self; 


[_audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 
} 

回答

0

也許你得檢查audioplayer是勞克之前已經打另一首歌曲

if(self.audioplayer && [self.audioplayer isPlaying]){ 
    [self.audioplayer stop]; 
} 
+0

我試過這不起作用 – iTag

+0

你必須寫信e在您的方法的頂部 - >「之前」self.audioPlayer = [[AVAudioPlayer alloc]「 –

相關問題