iOS 使用 ID3Lib 从 NSDocsDir 中的 mp3 文件编辑 ID3 标签

标签 ios cocoa-touch mp3 id3 nsdocumentdirectory

我正在使用 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);
}

最佳答案

使用 UTF-8 编码的文件路径,将正常工作:

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

关于iOS 使用 ID3Lib 从 NSDocsDir 中的 mp3 文件编辑 ID3 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299197/

相关文章:

ios - 如何在我没有源代码的应用程序上执行 UITesting?

ios - VsTac 任务失败,使用全局 Cordova 工具集时找不到文件 appAsTgz.tgz

ios - UIContextualAction 带有图标和清晰的背景 swift

javascript - Flash Lite 3.1 + javascript

java - 在java中暂停mp3

Java启动、停止和恢复线程

iphone - 如何根据时间取消 UILocalNotification

iphone - 如何停止 UITableView 在顶部和底部的滚动?

ios - NSData 到 NSString 编码

cocoa-touch - iOS - 应用内购买 & applicationDidBecomeActive