TagLib是KDE開源項目的一個很有用的C++函數庫,可以用於讀取各種格式的歌曲ID3資訊,諸如歌名、歌手、專輯等資訊。
1.5版的TagLib支援MacOS,我們可以在Cocoa中直接使用TagLib來擷取MP3歌曲的ID3資訊。就是使用TagLib擷取的歌曲資訊,沒有使用iTunes。
TagLib的功能強大,用法很簡單,只需要在工程中加入TagLib.framework,同時在需要使用TagLib的.h檔案中加入如下引用:
C代碼
NSString *aFile = [[NSBundle mainBundle] pathForResource:@"星座" ofType:@"mp3"];
TagLib::FileRef f([aFile UTF8String]);
TagLib::Tag *mytag=f.tag();
TagLib::AudioProperties *aproperties=f.audioProperties();
TagLib::String title = mytag->title();
TagLib::String author = mytag->artist();
TagLib::String album = mytag->album();
TagLib::String comment = mytag->comment();
TagLib::String genre = mytag->genre();
TagLib::uint year = mytag->year();
TagLib::uint track = mytag->track();
NSString *nsTitle=[NSString stringWithCString:title.toCString(true)
encoding:NSUTF8StringEncoding];
NSString *nsAuthor = [NSString stringWithCString:author.toCString(true)
encoding:NSUTF8StringEncoding];
NSString *nsAlbum = [NSString stringWithCString:album.toCString(true)
encoding:NSUTF8StringEncoding];
NSString *nsComment = [NSString stringWithCString:comment.toCString(true)
encoding:NSUTF8StringEncoding];
NSString *nsGenre = [NSString stringWithCString:genre.toCString(true)
encoding:NSUTF8StringEncoding];
NSLog(@"title:%@", nsTitle);
NSLog(@"author:%@", nsAuthor);
NSLog(@"album:%@", nsAlbum);
NSLog(@"comment:%@", nsComment);
NSLog(@"genre:%@", nsGenre);
NSLog(@"%d", year);
NSLog(@"track:%d", track);
運行結果請看附件!
作者“wsqwsq000”