2014-04-16 25 views
2

我遇到了AVPlayerItemAVQueuePlayer的问题。目前,我有很多音乐文件,这些文件的长度为1-2秒,并且有一个队列播放器可以按顺序播放它们。如何知道AVPlayerItem正在播放的时间

我想知道什么时候音乐文件刚刚开始播放而不是播放完毕时(通过AVPlayerItemDidPlayToEndTimeNotification)。

这是因为我想在加载和播放新文件时运行一个函数。

我的代码:

for (NSUInteger i = 0; i < [matchedAddr count]; i++) 
{ 
    NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:[matchedAddr objectAtIndex:i] ofType:@"wav"]; 
    //NSLog(@"file %@",firstVideoPath); 
    avitem=[AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(currentItemIs:) 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:avitem]; 

    [filelist addObject:avitem]; 
} 
player = [AVQueuePlayer queuePlayerWithItems:filelist]; 
[player play]; 


- (void)currentItemIs:(NSNotification *)notification 
{ 
    NSString *asd=[seqArray objectAtIndex:currentColor]; 
    currentColor=currentColor+1; 
    AVPlayerItem *p = [notification object]; 
    [p seekToTime:kCMTimeZero]; 

    if([asd isEqual:@"1"]) 
    { 
     [UIView animateWithDuration:0.01 animations:^{ 
      one.alpha = 0; 
     } completion:^(BOOL finished) { 

      [UIView animateWithDuration:0.01 animations:^{ 
       one.alpha = 1; 
      }]; 
     }]; 
    } 
} 

正如你所看到的,currentItemIs无效的调用,但它运行时,轨道已完成playing.I希望在赛道是在开始时被调用。

编辑: 更新温斯顿的片段的版本:

NSString * const kStatusKey   = @"status"; 

     [avitem addObserver:self 
           forKeyPath:kStatusKey 
           options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew 
           context:@"AVPlayerStatus"]; 
- (void)observeValueForKeyPath:(NSString *)path 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context { 

    if (context == @"AVPlayerStatus") { 

     AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; 
     switch (status) { 
      case AVPlayerStatusUnknown: { 

      } 
       break; 

      case AVPlayerStatusReadyToPlay: { 
       // audio will begin to play now. 
       NSLog(@"PLAU"); 
       [self playa]; 
      } 
       break; 
     } 
    } 
} 

回答

5

首先,你需要注册AVPlayerItem作为观察员

[self.yourPlayerItem addObserver:self 
         forKeyPath:kStatus 
         options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew 
         context:AVPlayerStatus]; 

然后在您的播放器键值观察您需要检查的方法AVPlayerStatusReadyToPlay状态,像这样:

- (void)observeValueForKeyPath:(NSString *)path 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context { 

    if (context == AVPlayerStatus) { 

     AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; 
     switch (status) { 
      case AVPlayerStatusUnknown: { 

      } 
      break; 

      case AVPlayerStatusReadyToPlay: { 
       // audio will begin to play now. 
      } 
      break; 
    } 
} 
+0

让我测试我会得到back2u。 – Theodoros80

+0

好的。我做了一些修改,请再次输入代码。谢谢。 – Winston

+0

谢谢,它的工作,我已经添加了我的最终代码更正。 – Theodoros80

相关问题