0

ViewController从另一个UITableViewController传递歌曲名称和索引的地方调用它。它第一次播放歌曲,但是当我返回tableView并单击另一首歌曲时,它同时播放两首歌曲。

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

1 回答 1

0

也许您必须检查音频播放器是否已经在播放另一首歌曲

if(self.audioplayer && [self.audioplayer isPlaying]){
    [self.audioplayer stop];
}
于 2013-07-23T09:14:32.013 回答