iOS использует ID3Lib для редактирования тегов ID3 ​​из файлов mp3 в NSDocsDir

Я использую пример проекта ID3Lib для редактирования тегов Title, Album и Artist ID3 в файлах mp3, и все это хорошо, пока я не добавлю изображение (обложку), если у кого-то есть идеи, как закончить приведенный ниже код, который был бы отличным:

- (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);
}

person gav    schedule 18.08.2013    source источник


Ответы (2)


Используйте путь к файлу с кодировкой UTF-8, и все будет нормально:

frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]);
person ILYA2606    schedule 17.04.2014

Я получил этот код из этого источника: 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

person Gonzalo Bahamondez    schedule 22.04.2014
comment
для обложки - frame4.GetField (ID3FN_PICTURETYPE) - ›Set (ID3PT_COVERFRONT); - person Sanniv; 13.03.2015