我从另一个传递歌曲名称和索引的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];
}
答案 0 :(得分:0)
也许你必须检查一下音乐播放器是否已经播放了另一首歌
if(self.audioplayer && [self.audioplayer isPlaying]){
[self.audioplayer stop];
}