2017-06-13 227 views
-1
- (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]; 
    } 

回答

0

使用默認方法暫停和播放音頻,檢查isPlaying?

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