iphone - 如何在 AVPlayer 中播放完第一首歌曲后继续播放第二首歌曲

标签 iphone objective-c ios xcode avplayer

如何使用 AVPlayer 立即开始下一首歌曲?我从 MPMediaItemCollection 中的 iPod 库中选择了歌曲集,显示在我的 UITableView [myPlaylistTable] 中。我知道我必须使用 NSNotification 来检查歌曲是否播放完毕,但我不知道如何在当前歌曲播放完毕后立即播放第二首或下一首歌曲。我在下面发布了我的代码

如果有人能帮助我解决问题,我将不胜感激。 谢谢

   in viewDidLoad i have initialized 

- (void)viewDidLoad
{
    [super viewDidLoad];

    //[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self.myPlaylistTable deselectRowAtIndexPath:[self.myPlaylistTable indexPathForSelectedRow] animated:NO];

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

    UInt32 doSetProperty = 0;
    AudioSessionSetProperty (
                             kAudioSessionProperty_OverrideCategoryMixWithOthers,
                             sizeof (doSetProperty),
                             &doSetProperty
                             );

    [self becomeFirstResponder];

    myPlaylistTable.hidden=YES;

    playlistSongsArray =[[NSMutableArray alloc]init];

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(nextSongPlaying:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[myPlayer currentItem]];

}      



-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

    // Dismiss selection view
    [self dismissViewControllerAnimated:YES completion:nil];


    // Get AVAsset
    NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];


    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];


    myPlayer = [AVPlayer playerWithPlayerItem:playerItem];

    [playlistSongsArray addObjectsFromArray:mediaItemCollection.items];

    NSLog(@" Playlist songs  %@",playlistSongsArray);

    [self.myPlaylistTable reloadData];

    [myPlayer play];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder 
{
    return YES;
}

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

    NSInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];


    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];


            UILabel *playbackDurationLabel =[[UILabel alloc]initWithFrame:CGRectMake(205, 88, 103, 18)];
            playbackDurationLabel.tag = 100;
            playbackDurationLabel.backgroundColor = [UIColor clearColor];
            playbackDurationLabel.textColor = [UIColor orangeColor];

            playbackDurationLabel.font = [UIFont fontWithName:@"Helvetica" size:14];

            [cell.contentView addSubview:playbackDurationLabel];

    }   

    if (tableView == myPlaylistTable)
    {
        MPMediaItem *mediaItem = (MPMediaItem *)[playlistSongsArray objectAtIndex:row]; 
        MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];


        if (mediaItem) 
        {
            cell.textLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
            cell.textLabel.textColor = [UIColor redColor];
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
            cell.textLabel.numberOfLines=0;

            cell.detailTextLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyArtist];
            cell.detailTextLabel.textColor = [UIColor blueColor];
            cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            cell.detailTextLabel.numberOfLines=0;

            UILabel *songDurationLabel = (UILabel *) [cell.contentView viewWithTag:100];
            songDurationLabel.text=[self secondsFormatted:[[mediaItem valueForProperty:MPMediaItemPropertyPlaybackDuration] intValue]];

            NSLog(@"song duration - %@",[[mediaItem valueForProperty:MPMediaItemPropertyPlaybackDuration] stringValue]);

            if (artwork != nil) 
            {
                cell.imageView.image = [artwork imageWithSize:CGSizeMake(60, 60)];
            }
            else
            {
                cell.imageView.image = [UIImage imageNamed:@"noArtworkImage.png"];
            }
        }   

        [tableView deselectRowAtIndexPath: indexPath animated: YES];
    }

    return cell;
}



    -(void)nextSongPlaying:(NSNotification *)notification
    {
         //What stuff to do 
    }

最佳答案

DShah 我是这样做的..你能检查它在我的 viewDidLoad 中是否正确吗..

     [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(nextSongPlaying:) 
name:AVPlayerItemDidPlayToEndTimeNotification 
object:[myPlayer currentItem]];

- (void)nextSongPlaying:(NSNotification *)notification 
{ 

AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero];
[myPlayer play];
}

关于iphone - 如何在 AVPlayer 中播放完第一首歌曲后继续播放第二首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11810293/

相关文章:

ios - 在 Interface Builder 中添加约束时 Xcode 无限期挂起

ios - 如何在 Swift 中通过部分标签文本搜索来定位 XCUIElement?

c# - EKEventStore 访问请求在 iOS 10 上崩溃

objective-c - NSDate 获取年/月/日

iphone - UINavigationController 过渡动画触发得太快

ios - 在 ios 中展开和折叠表格 View 单元格

ios - 保持iOS App唤醒以监控运动

iphone - iphone 设备之间的蓝牙信号强度

iphone - 在其他类(class)中对触摸使用react

iphone - 如何在 UILabel 上制作倒影?