iOS开发笔记—— 获取音频的专辑图与视频的缩略图

来源:互联网 发布:ubuntu搜狗输入法设置 编辑:程序博客网 时间:2024/05/12 16:49

iOS获取音频的专辑图与视频的缩略图  


http://www.brighttj.com/ios/get-audio-album-image-and-media-thumbnail-image.html

在制作音频播放器的时候,显示专辑图、艺术家、歌曲名等可能首先想到的是用字典封装。但实际上,音频文件中封装了这些数据,所以直接从mp3等文件中读取出来就可以了。而视频,系统的播放器类是没有缩略图显示的,需要自己读取。本文将讲解,如何获取音频和视频图片。

环境信息:

Mac OS X 10.9

Xcode:5.1.1

IOS 7.1.1

正文:

导入框架:

AVFoundation.framework

CoreMedia.framework

MediaPlayer.framework

一. 获取音频文件中的专辑图片

/**  *  通过音乐地址,读取音乐数据,获得图片  *   *  @param url 音乐地址   *   *  @return音乐图片*/ - (UIImage *)musicImageWithMusicURL:(NSURL *)url {       NSData *data = nil;       // 初始化媒体文件       AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:url options:nil];       // 读取文件中的数据       for (NSString *format in [mp3Asset availableMetadataFormats]) {              for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {                  //artwork这个key对应的value里面存的就是封面缩略图,其它key可以取出其它摘要信息,例如title - 标题                  if ([metadataItem.commonKey isEqualToString:@"artwork"]) {                       data = [(NSDictionary*)metadataItem.value objectForKey:@"data"];                       break;                  }             }       }       if (!data) {             // 如果音乐没有图片,就返回默认图片            return [UIImage imageNamed:@"default"];       }       return [UIImage imageWithData:data];}

当然,除了获取图片(data),还可以获取很多信息。如果有需要,可以NSLog以下这个集合,就知道了。

二. 视频首帧缩略图

/** *  通过视频的URL,获得视频缩略图* *  @param url 视频URL * *  @return首帧缩略图 */ - (UIImage *)imageWithMediaURL:(NSURL *)url {      NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]                                                        forKey:AVURLAssetPreferPreciseDurationAndTimingKey];       // 初始化媒体文件      AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];      // 根据asset构造一张图      AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];      // 设定缩略图的方向      // 如果不设定,可能会在视频旋转90/180/270°时,获取到的缩略图是被旋转过的,而不是正向的(自己的理解)      generator.appliesPreferredTrackTransform = YES;      // 设置图片的最大size(分辨率)      generator.maximumSize = CGSizeMake(600, 450);      // 初始化error      NSError *error = nil;      // 根据时间,获得第N帧的图片      // CMTimeMake(a, b)可以理解为获得第a/b秒的frame      CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(0, 10000) actualTime:NULL error:&error];      // 构造图片      UIImage *image = [UIImage imageWithCGImage: img];      return image; }

对于读视频首帧缩略图这个程序,有几个配置是按照自己理解来写的注释,所以为了准确性,下面我将苹果官方的解释和stakoverflow上的一些解释贴出来,大家综合参考。

1. generator.appliesPreferredTrackTransform = YES;

Specifies whether to apply the track matrix (or matrices) when extracting an image from the asset.

Discussion

The default is NO AVAssetImageGenerator only supports rotation by 90, 180, or 270 degrees.

This property is ignored if you set a value for the videoComposition property.

——developer.apple.com

To fix the thumbnail orientation set appliesPreferredTrackTransform to YES in the AVAssetImageGenerator instance. If you add your own video composition, you’ll need to include the right transform to rotate the video as wanted.

——http://stackoverflow.com/  djromero

2. CMTimeMake(010000)

1) CMTimeMake(1,10) actually means a value of 1 and a timescale of 10. They are a numerator and denominator, so it is 1/10 of a second, not 1 second.

2) The result will be like CMTimeMake(2, 10), which is 2/10ths of a second.

——http://stackoverflow.com/ Peter DeWeese

0 0