【问题标题】:how to continue playing second song after first song is finished playing in AVPlayer在AVPlayer中播放完第一首歌曲后如何继续播放第二首歌曲
【发布时间】:2012-08-04 16:46:37
【问题描述】:

如何使用 AVPlayer 立即开始下一首歌曲?我在我的 UITableView [myPlaylistTable] 中显示了从 MPMediaItemCollection 中的 iPod 库中选择的歌曲集合。我知道我必须使用 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 
    }

【问题讨论】:

    标签: iphone objective-c ios xcode avplayer


    【解决方案1】:

    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];
    }
    

    【讨论】:

    【解决方案2】:

    请参阅 Apple 的链接以获取帮助:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html

    此链接解释了播放多个项目。您可以使用AVQueuePlayer 的子类AVPlayer 来对多首歌曲进行排队。

    编辑

    请查看 GitHub 链接:https://github.com/crocodella/AVPlayerSample 解释:http://www.crocodella.com.br/2011/01/using-avplayer-to-play-background-music-from-library/

    这个链接和你想要的一样。

    【讨论】:

    • NSArray *items = ; AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] initWithItems:items]; AVPlayerItem *anItem = ; if ([queuePlayer canInsertItem:anItem afterItem:nil]) { [queuePlayer insertItem:anItem afterItem:nil];由于我从 ipod 库中获取歌曲并将其放在自定义 tableview 上,它如何知道歌曲已完成并且下一首歌曲必须继续
    • 因为你已经为 AVPlayerItemDidPlayToEndTimeNotification 注册了通知,所以只要歌曲完成,你的选择器方法就会被触发...
    • 是的,我不知道如何获取下一首歌曲并在当前歌曲播放完毕后开始播放
    • 与您开始第一首歌曲的方式相同,但唯一的区别是现在您将在选择器方法中编写逻辑。每当第一首歌曲结束时处理 Selector 方法。
    • 参考链接中的响应状态变化部分,注册了一个KVO。它类似于通知(记住但与通知不同)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多