完成后,AVPlayer前进到下一个流URL

时间:2015-03-04 19:07:14

标签: ios objective-c avplayer

我有一张包含Soundcloud曲目的表格视图。选择行时,轨道流。当轨道完成时,如何让它自动前进到下一个单元格中的轨道?

以下是一些代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    }

    if (cell)
    {
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.numberOfLines = 2;

        if (indexPath.row == selectedRow)
        {
            cell.imageView.image = [UIImage imageNamed:@"audio_wave"];
            UIImage *musicOne = [UIImage imageNamed:@"audio_wave"];
            UIImage *musicTwo = [UIImage imageNamed:@"audio_wave_2"];
            UIImage *musicThree = [UIImage imageNamed:@"audio_wave_3"];
            UIImage *musicFour = [UIImage imageNamed:@"audio_wave_4"];

            NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, musicFour, nil];

            cell.imageView.animationImages = imagesArray;
            cell.imageView.animationDuration = 0.62;
            cell.imageView.animationRepeatCount = 0;
            [cell.imageView startAnimating];
        }
        else
        {
            cell.imageView.image = nil;
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *streamingString = [NSString stringWithFormat:@"%@.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
    NSURL *streamingURL = [NSURL URLWithString:streamingString];
    selectedRow = indexPath.row;
    [tableView reloadData];
    player = [AVPlayer playerWithURL:streamingURL];
    [player play];
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}

1 个答案:

答案 0 :(得分:3)

添加AVPlayerItemDidPlayToEndTimeNotification观察者以触发更改,例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *streamingString = [NSString stringWithFormat:@"%@.json?client_id=fc886d005e29ba78f046e5474e3fdefb", [streamURLArray objectAtIndex:indexPath.row]];
    NSURL *streamingURL = [NSURL URLWithString:streamingString];
    selectedRow = indexPath.row;
    [tableView reloadData];

    player = [AVPlayer playerWithURL:streamingURL];
    [player play];
    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

    [[NSNotificationCenter defaultCenter] addObserver:self
                      selector:@selector(goToNextTrack)
                      name:AVPlayerItemDidPlayToEndTimeNotification
                      object:nil];
}

- (void)goToNextTrack {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                      name:AVPlayerItemDidPlayToEndTimeNotification
                      object:nil];
    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow+1 inSection:0]];
}