获取音频文件的ID3信息,歌手名称,专辑名称,专辑图片等信息,支持IOS5以上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
NSBundle
*
bundle
=
[
NSBundle
mainBundle
]
;
NSString
*
path
=
[
bundle
bundlePath
]
;
NSURL
*
fileURL
=
[
NSURL
fileURLWithPath
:
[
NSString
stringWithFormat
:
@"%@/akon、be - you - with.mp3"
,
path
]
]
;
AudioFileTypeID
fileTypeHint
=
kAudioFileMP3Type
;
NSString
*fileExtension
=
[
[
fileURL
path
]
pathExtension
]
;
if
(
[
fileExtension
isEqual
:
@"mp3"
]
||
[
fileExtension
isEqual
:
@"m4a"
]
)
{
AudioFileID
fileID
=
nil
;
OSStatus
err
=
noErr
;
err
=
AudioFileOpenURL
(
(
CFURLRef
)
fileURL
,
kAudioFileReadPermission
,
0
,
&
fileID
)
;
if
(
err
!=
noErr
)
{
NSLog
(
@"AudioFileOpenURL failed"
)
;
}
UInt32
id3DataSize
=
0
;
err
=
AudioFileGetPropertyInfo
(
fileID
,
kAudioFilePropertyID3Tag
,
&
id3DataSize
,
NULL
)
;
if
(
err
!=
noErr
)
{
NSLog
(
@"AudioFileGetPropertyInfo failed for ID3 tag"
)
;
}
NSDictionary
*piDict
=
nil
;
UInt32
piDataSize
=
sizeof
(
piDict
)
;
err
=
AudioFileGetProperty
(
fileID
,
kAudioFilePropertyInfoDictionary
,
&
piDataSize
,
&
piDict
)
;
if
(
err
!=
noErr
)
{
[
piDict
release
]
;
NSLog
(
@"AudioFileGetProperty failed for property info dictionary"
)
;
}
CFDataRef
AlbumPic
=
nil
;
UInt32
picDataSize
=
sizeof
(
picDataSize
)
;
err
=
AudioFileGetProperty
(
fileID
,
kAudioFilePropertyAlbumArtwork
,
&
picDataSize
,
&
AlbumPic
)
;
if
(
err
!=
noErr
)
{
NSLog
(
@"Get picture failed"
)
;
}
NSData
*
imagedata
=
(
NSData
*
)
AlbumPic
;
UIImage
*
image
=
[
[
UIImage
alloc
]
initWithData
:imagedata
]
;
imageView
.
image
=
image
;
NSString
*
Album
=
[
(
NSDictionary
*
)
piDict
objectForKey
:
[
NSString
stringWithUTF8String
: kAFInfoDictionary_Album
]
]
;
NSString
*
Artist
=
[
(
NSDictionary
*
)
piDict
objectForKey
:
[
NSString
stringWithUTF8String
: kAFInfoDictionary_Artist
]
]
;
NSString
*
Title
=
[
(
NSDictionary
*
)
piDict
objectForKey
:
[
NSString
stringWithUTF8String
: kAFInfoDictionary_Title
]
]
;
NSLog
(
@"%@"
,
Title
)
;
NSLog
(
@"%@"
,
Artist
)
;
NSLog
(
@"%@"
,
Album
)
;
}
|
方法二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//取出资源文件下的所有mp3路径
NSArray
*mp3Array
=
[
NSBundle
pathsForResourcesOfType
:
@"mp3"
inDirectory
:
[
[
NSBundle
mainBundle
]
resourcePath
]
]
;
for
(
NSString
*filePath
in
mp3Array
)
{
//IOS的资源路径封装,网络文件用urlWithPath:
NSURL
*fileURL
=
[
NSURL
fileURLWithPath
:filePath
]
;
AVURLAsset
*mp3Asset
=
[
AVURLAsset
URLAssetWithURL
:fileURL
options
:nil
]
;
for
(
NSString
*format
in
[
mp3Asset
availableMetadataFormats
]
)
{
NSLog
(
@"format type = %@"
,
format
)
;
for
(
AVMetadataItem
*metadataItem
in
[
mp3Asset
metadataForFormat
:format
]
)
{
NSLog
(
@"commonKey = %@"
,
metadataItem
.
commonKey
)
;
//artwork这个key对应的value里面存的就是封面缩略图,其它key可以取出其它摘要信息,例如title - 标题
//title - 标题;artwork - 缩略图;albumName - 专辑名;artist - 艺术家;artwork - 缩略图
if
(
[
metadataItem
.
commonKey
isEqualToString
:
@"artwork"
]
)
{
NSData
*data
=
[
(
NSDictionary
*
)
metadataItem
.
value
objectForKey
:
@"data"
]
;
NSString
*mime
=
[
(
NSDictionary
*
)
metadataItem
.
value
objectForKey
:
@"MIME"
]
;
NSLog
(
@"mime = %@, data = %@, image = %@"
,
mime
,
data
,
[
UIImage
imageWithData
:data
]
)
;
break
;
}
}
}
}
//时间
CMTime
duration
=
mp3Asset
.
duration
;
int
seconds
=
(
int
)
CMTimeGetSeconds
(
duration
)
;
//总时间
int
minute
=
0
;
if
(
seconds
>=
60
)
{
int
index
=
seconds
/
60
;
minute
=
index
;
seconds
=
seconds
-
index
*
60
;
}
NSString
*mTime
=
[
NSString
stringWithFormat
:
@"%i:%i"
,
minute
,
seconds
]
;
|