1

I am using AVAudioPlayer to do audio streaming. Now I'd like to display some audio metadata (eg. artist, albumName, title and artwork) on lock screen. However I'm getting an issue like following:

• if I use the statement

NSURL *url = [NSURL URLWithString:urlString];

to create the URL, then I can obtain the metadata. But the audio player stops (just plays a few seconds of the audio) and doesn't finish the audio.

• if I use the statement

NSURL *url = [[NSURL alloc] initWithString:urlString];

to create the URL, the audio player works fine. But the code doesn't get into the for (NSString *format in [asset availableMetadataFormats]) loop to obtain the metadata.

Below is the piece of code I am using to obtain the metadata. Would like to know what the difference is between the above two NSURL creations. And how to fix this issue so the audio player can work fine when obtaining the metadata. Thanks a lot in advance.

Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

if (playingInfoCenter)
{
    //NSURL *url = [NSURL URLWithString:urlString];
    NSURL *url = [[NSURL alloc] initWithString:urlString];

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

    for (NSString *format in [asset availableMetadataFormats])
    {
        for (AVMetadataItem *metadataItem in [asset metadataForFormat:format])
        {
            if ([metadataItem.commonKey isEqualToString:@"artwork"])
            {
                UIImage* image = [UIImage imageWithData:[(NSDictionary*)metadataItem.value objectForKey:@"data"]];
                MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:image];
                [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
           }
           else if ([metadataItem.commonKey isEqualToString:@"title"])
           {
                [songInfo setObject:metadataItem.value forKey:MPMediaItemPropertyTitle];
           }
           else if ([metadataItem.commonKey isEqualToString:@"albumName"])
           {
               [songInfo setObject:metadataItem.value forKey:MPMediaItemPropertyAlbumTitle];
           }
           else if ([metadataItem.commonKey isEqualToString:@"artist"])
           {
               [songInfo setObject:metadataItem.value forKey:MPMediaItemPropertyArtist];
           }
      }
 }

 [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
4

1 回答 1

0

首先,你用的是ARC吗?如果不是,那么这两个变量之间存在差异。如果[NSURL URLWithString:urlString]您获得一个自动释放的对象(应该在不再使用时自动释放)并且[[NSURL alloc] initWithString:urlString]您成为对象的所有者(您保留它)并且您必须release在不需要它时调用它。

但是,有了for (NSString *format in [asset availableMetadataFormats])问题,如果代码没有进入for语句,则可能意味着以下三种情况之一:

  1. asset为 nil,如果您传入的 URL[AVURLAsset URLAssetWithURL:url options:nil]未指向有效文件,则会发生这种情况。
  2. availableMetadataFormats为零,该资产没有任何可用的元数据格式。
  3. 返回的数组availableMetadataFormats不是 nil,而是空的。

很抱歉没有告诉您更多信息,但我需要有关应用程序状态的更多信息。这些建议可能只是帮助您找到问题所在。

于 2013-08-27T16:35:28.580 回答