1

我正在使用ID3Lib示例项目来编辑 mp3 文件上的标题、专辑和艺术家 ID3 标签,一切都很好,直到我来添加图像(封面艺术),如果有人知道如何完成下面的代码,那就太好了:

- (void)demo {


nImage = [UIImage imageNamed:@"amazing-grace.jpg"];//unused

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"amazing-grace" ofType:@"jpg"];

NSString *path = [[NSBundle mainBundle] pathForResource:@"amazing-grace-10s" ofType:@"mp3"];

NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"amazing-grace-10s.mp3"];

[[NSFileManager defaultManager] copyItemAtPath:path toPath:docPath error:nil];

// Read title tag
ID3_Tag tag;
tag.Link([path UTF8String]);

ID3_Frame *titleFrame = tag.Find(ID3FID_TITLE);
unicode_t const *value = titleFrame->GetField(ID3FN_TEXT)->GetRawUnicodeText();
NSString *title = [NSString stringWithCString:(char const *) value encoding:NSUnicodeStringEncoding];
NSLog(@"The title before is %@", title);


// Write title tag
tag.Link([docPath UTF8String]);
tag.Strip(ID3FID_TITLE);
tag.Strip(ID3FID_ALBUM);
tag.Strip(ID3FID_LEADARTIST);
tag.Clear();

ID3_Frame frame;
frame.SetID(ID3FID_TITLE);
frame.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newTitle = nTitle;
frame.GetField(ID3FN_TEXT)->Set((unicode_t *) [newTitle cStringUsingEncoding:NSUTF16StringEncoding]);

ID3_Frame frame2;
frame2.SetID(ID3FID_ALBUM);
frame2.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newAlbum = nAlbmum;
frame2.GetField(ID3FN_TEXT)->Set((unicode_t *) [newAlbum cStringUsingEncoding:NSUTF16StringEncoding]);

ID3_Frame frame3;
frame3.SetID(ID3FID_LEADARTIST);
frame3.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);
NSString *newArtist = nArtist;
frame3.GetField(ID3FN_TEXT)->Set((unicode_t *) [newArtist cStringUsingEncoding:NSUTF16StringEncoding]);

//this is the image code 
ID3_Frame frame4;
frame4.SetID(ID3FID_PICTURE);
frame4.GetField(ID3FN_TEXTENC)->Set(ID3TE_UNICODE);// dont think this should be TEXTENC
NSString *newImage = imagePath;
frame4.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF16StringEncoding]);//this line is also probably wrong


tag.AddFrame(frame);
tag.AddFrame(frame2);
tag.AddFrame(frame3);
tag.AddFrame(frame4);

tag.SetPadding(false);
tag.SetUnsync(false);
tag.Update(ID3TT_ID3V2);

NSLog(@"The title after is %@ The album after is %@ The artist after is %@ The artist after is %@", newTitle,newAlbum,newArtist,newImage);
}
4

2 回答 2

1

我得到这个代码,来自这个来源:https ://android.googlesource.com/platform/external/id3lib/+/8a58cac9f8db5ed55b02b3ac24156d628af923d1/examples/test_pic.cpp

frame.SetID(ID3FID_PICTURE);
frame.GetField(ID3FN_MIMETYPE)->Set("image/jpeg");
frame.GetField(ID3FN_PICTURETYPE)->Set(11);
frame.GetField(ID3FN_DESCRIPTION)->Set("B/W picture of Saint-Saëns");
//frame.GetField(ID3FN_DATA)->FromFile("composer.jpg");
frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]);

tag.AddFrame(frame);

和!这项工作很好。:D

于 2014-04-22T19:07:09.957 回答
1

使用带有 UTF-8 编码的文件路径并且可以正常工作:

frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]);
于 2014-04-17T07:42:16.997 回答