AVAudioPlayer同时播放多首歌曲?

时间:2013-07-23 05:18:33

标签: objective-c avfoundation avaudioplayer

我从另一个传递歌曲名称和索引的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];
}

1 个答案:

答案 0 :(得分:0)

也许你必须检查一下音乐播放器是否已经播放了另一首歌

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