iOS获取本地音频文件(属性/信息)
程序员文章站
2023-12-10 15:54:53
本文实例为大家分享了ios获取本地音频文件的具体代码,供大家参考,具体内容如下
获取本地音频文件地址:
nsstring *songsdirectory=m...
本文实例为大家分享了ios获取本地音频文件的具体代码,供大家参考,具体内容如下
获取本地音频文件地址:
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 nil]; nsarray *temparrinfo = [[nsarray alloc] initwithobjects:singer,song,albumname,filesize,voicestyle,filestyle,creatdate,savepath, nil 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)]; } }
附图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。