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

AVPlayer Advance to Next Stream URL when Finished

我有一个 table 视图,其中包含来自 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;
}

添加一个 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]];
}