如何暂停和播放音频?

时间:2017-06-13 05:34:00

标签: ios objective-c uitableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellId = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    songs = [MPMediaQuery songsQuery];
    songsArray = [songs items];
    MPContentItem *item = [songsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [item valueForKey:@"title"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


   MPContentItem *item = [songsArray objectAtIndex:indexPath.row];

    if ([self.AudioPlayer isPlaying]) {

        [self.AudioPlayer pause];

    }else if(![self.AudioPlayer isPlaying]){

                NSError *error;
                self.AudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[item valueForKey:MPMediaItemPropertyAssetURL] error:&error];
                [self.AudioPlayer play];
    }

1 个答案:

答案 0 :(得分:0)

使用默认方法暂停和播放音频,检查isPlaying?

if([AudioPlayerName isPlaying])
 {
  [AudioPlayerName Pause];
 }
else
 {
  [AudioPlayerName Play];
 }