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