AVAudioPlayer, MPNowPlayingInfoCenter and audio metadata
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];
}
First of all, are you using ARC? If not, then there is a difference between these two variables. In the case of [NSURL URLWithString:urlString]
you are getting an autoreleased object (should get automatically released whenever not used any more) and with [[NSURL alloc] initWithString:urlString]
you become the owner of the object (you retained it) and you have to call release
on it whenever you don't need it.
However, with the for (NSString *format in [asset availableMetadataFormats])
problem, if the code does not enter the for statement, it can mean one of the following three things:
asset
is nil, which would happen if the URL you passed into [AVURLAsset URLAssetWithURL:url options:nil]
did not point to a valid file. availableMetadataFormats
is nil, that asset doesn't have any available metadata formats. availableMetadataFormats
is not nil, but empty. I am sorry for not telling you more, but I need more information about the state of the app. These suggestions might just help you find the problem.
链接地址: http://www.djcxy.com/p/66064.html上一篇: 将元数据写入mp3本地文件