MPMoviePlayerController timedMetadata在iOS 5中返回空白

时间:2012-02-01 06:40:17

标签: ios5 mpmovieplayercontroller audio-streaming

我使用MPMoviePlayerController来传输音频。我还使用其timedMetadata属性来获取mp3歌曲的ID3标签。这在iOS 4.x中运行良好,但在iOS 5中运行不正确。

以下是我使用的代码:

MPMoviePlayerController* streamPlayer;    
// allocation and initialization code ...

- (void) metadataUpdate: (id) sender {
NSLog(@"GOT METADATA!!!!!");
if ([streamPlayer timedMetadata]!=nil && [[streamPlayer timedMetadata] count] > 0) {
    NSLog(@"metadata count = %d", [[streamPlayer timedMetadata] count]);
    for (MPTimedMetadata *metadata in [streamPlayer timedMetadata]) {
        NSLog(@"description %@", metadata.allMetadata);
        if ([[metadata.allMetadata valueForKey:@"key"] isEqualToString:@"title"]) {
            song.text = [metadata.allMetadata valueForKey:@"value"];
            filename = song.text;
        }
    }
}

} 

更具体地说,在iOS 5下,metadata [allMetadata]在上面的代码中返回空白,而[[streamPlayer timedMetadata] count]为2。

这是iOS 5中的一个错误吗?

2 个答案:

答案 0 :(得分:2)

我看到了相同的行为,并会提交错误报告。与此同时,我只是建立自己的字典来传递。

- (void)metadataUpdate:(NSNotification *)notification  
{
    NSMutableDictionary *metaDict = [NSMutableDictionary dictionary];

    if ([self.moviePlayer timedMetadata]!=nil && [[self.moviePlayer timedMetadata] count] > 0) 
    {
        for (MPTimedMetadata *metadata in [self.moviePlayer timedMetadata]) 
        {
            [metaDict setObject:[metadata valueForKey:@"value"] forKey:[metadata valueForKey:@"key"]];
        }
        NSLog(@"Meta value:%@", metaDict);
    }
}

编辑: 为了响应playableDuration问题,下面为我返回playableDuration。但请注意,这不是视频的总时间......此时可播放的内容会根据已下载的内容而变化。 (在HTTP直播流的情况下)

- (void)viewDidLoad
{
    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:<your URL>];

    [self.moviePlayer.view setFrame: self.movieView.bounds];
    [self.movieView addSubview: self.moviePlayer.view];

    self.moviePlayer.shouldAutoplay = NO;
    [self.moviePlayer prepareToPlay];

}

- (IBAction)playGame:(UIButton *)sender
{
    if (self.moviePlayer.isPreparedToPlay) {
        [self.moviePlayer play];
        NSLog(@"%f", self.moviePlayer.playableDuration);
    }
}

答案 1 :(得分:1)

发现问题: MPTimedMetadata的allMetadata属性是罪魁祸首。出于某种原因,此属性在iOS 5中返回空,而在iOS 4中,它的工作方式如文档中所述 对我有用的正确代码是:

- (void) metadataUpdate: (id) sender {
  NSLog(@"GOT METADATA!!!!!");
  if ([streamPlayer timedMetadata]!=nil && [[streamPlayer timedMetadata] count] > 0) {
    for (MPTimedMetadata *metadata in [streamPlayer timedMetadata]) {
        if ([[metadata valueForKey:@"key"] isEqualToString:@"title"]) {
            song.text = [metadata valueForKey:@"value"];
            filename = song.text;
        }
    }
  }
}