IOS开发——获取本地音频文件(属性/信息)
2015-12-25 17:14
591 查看
获取本地音频文件地址:
NSString *songsDirectory=MUSIC_FILE_ALL;//沙盒地址
NSBundle *songBundle=[NSBundle bundleWithPath:songsDirectory];
NSString *bundlePath=[songBundle resourcePath];
NSArray *arrMp3=[NSBundle pathsForResourcesOfType:@"mp3" inDirectory:bundlePath];
for (NSString *filePath in arrMp3) {
[self.wMp3URL addObject:filePath];
}
解析音频文件属性:
-(void)mDefineUpControl{
NSString *filePath = [self.wMp3URL objectAtIndex: 0 ];//随便取一个,说明
//文件管理,取得文件属性
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *dictAtt = [fm attributesOfItemAtPath:filePath error:nil];
//取得音频数据
NSURL *fileURL=[NSURL fileURLWithPath:filePath];
AVURLAsset *mp3Asset=[AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *singer;//歌手
NSString *song;//歌曲名
UIImage *image;//图片
NSString *albumName;//专辑名
NSString *fileSize;//文件大小
NSString *voiceStyle;//音质类型
NSString *fileStyle;//文件类型
NSString *creatDate;//创建日期
NSString *savePath; //存储路径
for (NSString *format in [mp3Asset availableMetadataFormats]) {
for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {
if([metadataItem.commonKey isEqualToString:@"title"]){
song = (NSString *)metadataItem.value;//歌曲名
}else if ([metadataItem.commonKey isEqualToString:@"artist"]){
singer = (NSString *)metadataItem.value;//歌手
}
// 专辑名称
else if ([metadataItem.commonKey isEqualToString:@"albumName"])
{
albumName = (NSString *)metadataItem.value;
}else if ([metadataItem.commonKey isEqualToString:@"artwork"]) {
NSDictionary *dict=(NSDictionary *)metadataItem.value;
NSData *data=[dict objectForKey:@"data"];
image=[UIImage imageWithData:data];//图片
}
}
}
savePath = filePath;
float tempFlo = [[dictAtt objectForKey:@"NSFileSize"] floatValue]/(1024*1024);
fileSize = [NSString stringWithFormat:@"%.2fMB",[[dictAtt objectForKey:@"NSFileSize"] floatValue]/(1024*1024)];
NSString *tempStrr = [NSString stringWithFormat:@"%@", [dictAtt objectForKey:@"NSFileCreationDate"]] ;
creatDate = [tempStrr substringToIndex:19];
fileStyle = [filePath substringFromIndex:[filePath length]-3];
if(tempFlo <= 2){
voiceStyle = @"普通";
}else if(tempFlo > 2 && tempFlo <= 5){
voiceStyle = @"良好";
}else if(tempFlo > 5 && tempFlo < 10){
voiceStyle = @"标准";
}else if(tempFlo > 10){
voiceStyle = @"高清";
}
NSArray *tempArr = [[NSArray alloc] initWithObjects:@"歌手:",@"歌曲名称:",@"专辑名称:",@"文件大小:",@"音质类型:",@"文件格式:",@"创建日期:",@"保存路径:", nil];
NSArray *tempArrInfo = [[NSArray alloc] initWithObjects:singer,song,albumName,fileSize,voiceStyle,fileStyle,creatDate,savePath, nil];
for(int i = 0;i < [tempArr count]; i ++){
NSString *strTitle = [tempArr objectAtIndex:i];
UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(5, 5+i*30, 16*[strTitle length], 25)];
[titleLab setText:strTitle];
[titleLab setTextColor:[WASharedFontStyle mGetSharedFontColor]];
[titleLab setFont:[UIFont systemFontOfSize:16]];
[self.wInfoSV addSubview:titleLab];
NSString *strInfo = [tempArrInfo objectAtIndex:i];
UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(titleLab.frame.origin.x+titleLab.bounds.size.width+5, 5+i*30, self.view.bounds.size.width-(titleLab.frame.origin.x+titleLab.bounds.size.width+5)-5, 25)];
[infoLab setText:strInfo];
[infoLab setTextColor:[WASharedFontStyle mGetSharedFontColor]];
[infoLab setFont:[UIFont systemFontOfSize:16]];
[self.wInfoSV addSubview:infoLab];
if(i == [tempArr count]-1){
[infoLab setFrame:CGRectMake(titleLab.frame.origin.x+titleLab.bounds.size.width+5, 5+i*30, self.view.bounds.size.width-(titleLab.frame.origin.x+titleLab.bounds.size.width+5)-5, 30*4)];
[infoLab setLineBreakMode:NSLineBreakByWordWrapping];
[infoLab setFont:[UIFont systemFontOfSize:12]];
[infoLab setNumberOfLines:0];
}
[self.wInfoSV setContentSize:CGSizeMake(self.view.bounds.size.width, i*45)];
}
}
附图:
NSString *songsDirectory=MUSIC_FILE_ALL;//沙盒地址
NSBundle *songBundle=[NSBundle bundleWithPath:songsDirectory];
NSString *bundlePath=[songBundle resourcePath];
NSArray *arrMp3=[NSBundle pathsForResourcesOfType:@"mp3" inDirectory:bundlePath];
for (NSString *filePath in arrMp3) {
[self.wMp3URL addObject:filePath];
}
解析音频文件属性:
-(void)mDefineUpControl{
NSString *filePath = [self.wMp3URL objectAtIndex: 0 ];//随便取一个,说明
//文件管理,取得文件属性
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *dictAtt = [fm attributesOfItemAtPath:filePath error:nil];
//取得音频数据
NSURL *fileURL=[NSURL fileURLWithPath:filePath];
AVURLAsset *mp3Asset=[AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *singer;//歌手
NSString *song;//歌曲名
UIImage *image;//图片
NSString *albumName;//专辑名
NSString *fileSize;//文件大小
NSString *voiceStyle;//音质类型
NSString *fileStyle;//文件类型
NSString *creatDate;//创建日期
NSString *savePath; //存储路径
for (NSString *format in [mp3Asset availableMetadataFormats]) {
for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {
if([metadataItem.commonKey isEqualToString:@"title"]){
song = (NSString *)metadataItem.value;//歌曲名
}else if ([metadataItem.commonKey isEqualToString:@"artist"]){
singer = (NSString *)metadataItem.value;//歌手
}
// 专辑名称
else if ([metadataItem.commonKey isEqualToString:@"albumName"])
{
albumName = (NSString *)metadataItem.value;
}else if ([metadataItem.commonKey isEqualToString:@"artwork"]) {
NSDictionary *dict=(NSDictionary *)metadataItem.value;
NSData *data=[dict objectForKey:@"data"];
image=[UIImage imageWithData:data];//图片
}
}
}
savePath = filePath;
float tempFlo = [[dictAtt objectForKey:@"NSFileSize"] floatValue]/(1024*1024);
fileSize = [NSString stringWithFormat:@"%.2fMB",[[dictAtt objectForKey:@"NSFileSize"] floatValue]/(1024*1024)];
NSString *tempStrr = [NSString stringWithFormat:@"%@", [dictAtt objectForKey:@"NSFileCreationDate"]] ;
creatDate = [tempStrr substringToIndex:19];
fileStyle = [filePath substringFromIndex:[filePath length]-3];
if(tempFlo <= 2){
voiceStyle = @"普通";
}else if(tempFlo > 2 && tempFlo <= 5){
voiceStyle = @"良好";
}else if(tempFlo > 5 && tempFlo < 10){
voiceStyle = @"标准";
}else if(tempFlo > 10){
voiceStyle = @"高清";
}
NSArray *tempArr = [[NSArray alloc] initWithObjects:@"歌手:",@"歌曲名称:",@"专辑名称:",@"文件大小:",@"音质类型:",@"文件格式:",@"创建日期:",@"保存路径:", nil];
NSArray *tempArrInfo = [[NSArray alloc] initWithObjects:singer,song,albumName,fileSize,voiceStyle,fileStyle,creatDate,savePath, nil];
for(int i = 0;i < [tempArr count]; i ++){
NSString *strTitle = [tempArr objectAtIndex:i];
UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(5, 5+i*30, 16*[strTitle length], 25)];
[titleLab setText:strTitle];
[titleLab setTextColor:[WASharedFontStyle mGetSharedFontColor]];
[titleLab setFont:[UIFont systemFontOfSize:16]];
[self.wInfoSV addSubview:titleLab];
NSString *strInfo = [tempArrInfo objectAtIndex:i];
UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(titleLab.frame.origin.x+titleLab.bounds.size.width+5, 5+i*30, self.view.bounds.size.width-(titleLab.frame.origin.x+titleLab.bounds.size.width+5)-5, 25)];
[infoLab setText:strInfo];
[infoLab setTextColor:[WASharedFontStyle mGetSharedFontColor]];
[infoLab setFont:[UIFont systemFontOfSize:16]];
[self.wInfoSV addSubview:infoLab];
if(i == [tempArr count]-1){
[infoLab setFrame:CGRectMake(titleLab.frame.origin.x+titleLab.bounds.size.width+5, 5+i*30, self.view.bounds.size.width-(titleLab.frame.origin.x+titleLab.bounds.size.width+5)-5, 30*4)];
[infoLab setLineBreakMode:NSLineBreakByWordWrapping];
[infoLab setFont:[UIFont systemFontOfSize:12]];
[infoLab setNumberOfLines:0];
}
[self.wInfoSV setContentSize:CGSizeMake(self.view.bounds.size.width, i*45)];
}
}
附图:
相关文章推荐
- IOS平台音频处理API介绍
- iOS 真机调试双击.cer证书
- iOS可变数组添加元素 以及可变数组操作
- iOS项目重构周记(一)
- iOS项目重构周记(二)
- iOS的三种多线程技术
- iOS开发笔记--连续跳转上层页面
- iOS从xib界面跳转到storyboard 中的界面和从storyboard中的界面跳转到另一个storyboard中的界面
- iOS--代码关联storyboard
- 处理动态类型的方法
- iOS 触摸事件五:手势处理器
- iOS学习之 plist文件的读写
- OC里面的集中数组遍历方法
- iOS开发-NSDate获取当前时区时间
- iOS中KVC和KVO有哪些用法?
- IOS 多媒体加载 加载音频 AVAudioPlayer
- iOS开发-调整文字之间间距
- iOS 去掉百度地图的 Logo(个人娱乐)
- 我的第一次份实习工作-iOS实习生-第三个月
- iOS报错:does not contain bitcode