2013-08-18 66 views
1

我使用ID3Lib示例項目來編輯mp3文件上的標題,專輯和藝術家ID3標籤,並且一切都很好,直到我添加一個圖像(封面Art)if任何人有任何想法如何幹掉下面的代碼,這將是巨大的:使用UTF-8編碼iOS使用ID3Lib編輯NSDocsDir中的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); 
} 

回答

1

使用文件的路徑和將正常工作:

frame.GetField(ID3FN_DATA)->FromFile((const char *)[newImage cStringUsingEncoding:NSUTF8StringEncoding]); 
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

+0

您應該設置爲frame4.GetField(ID3FN_PICTURETYPE) - > Set(ID3PT_COVERFRONT); – Sanniv