libid3标签在Objective-C中的使用



我正在尝试用Objective-C创建LAME mp3编码器GUI。我从iTunes-LAME.app学到了很多东西,复制了很多代码。这次,我从iTune-LAME.app复制并修改了id3tag编写函数。但在复制和修改的函数中,我得到了2字节语言的损坏字符。我想它可能有什么问题,但我不知道出了什么问题。这是函数。请帮帮我!!

编辑:我修改后的id3tag写入功能适用于单字节语言和数值。

struct id3_tag *id3tag;
struct id3_frame *frame = NULL;
frame = id3_frame_new(ID3_FRAME_TITLE);
id3tag = id3_tag_new();
id3_tag_attachframe(id3tag, frame);
id3_ucs4_t *ucs4 = id3_utf8_ucs4duplicate((const id3_utf8_t*)@"日本語".UTF8String);
if (id3_field_setstrings(&frame->fields[1], 1, &ucs4) == -1) {
    NSLog(@"Unable to set frame %s",ID3_FRAME_TITLE);
    id3_frame_delete(frame);
}
id3tag->options = 0;
id3tag->flags |= ID3_TAG_FLAG_FOOTERPRESENT;
id3_length_t len = id3_tag_render(id3tag, NULL);
char *buf = (char*)malloc(len);
len = id3_tag_render(id3tag, (id3_byte_t *)buf);
const char *filename=@"/Users/wayfarer/Desktop/01 ヴィーナスは眠れない.mp3".UTF8String;
int fd = open(filename, O_RDWR, 0644);
long fsize = lseek(fd, 0, SEEK_END);
long p = fsize;
int chunkSize = 1024 * 128;
buf = (char*)malloc(chunkSize);
while (p > 0) {
    p -= chunkSize;
    if (p < 0) {
        chunkSize += p;
        p = 0;
    }
    pread(fd, buf, chunkSize, p);
    pwrite(fd, buf, chunkSize, p+len);
}
lseek(fd, 0, SEEK_SET);
write(fd, buf, len);
free(buf);
close(fd);
id3_tag_delete(id3tag);

通过添加

id3_field_settextencoding(&frame->fields[0], ID3_FIELD_TEXTENCODING_UTF_8);

线下

id3_ucs4_t *ucs4 = id3_utf8_ucs4duplicate((const id3_utf8_t*)@"日本語".UTF8String);

我能解决这个问题。我不知道为什么iTunes-LAME.app可以在没有"id3_field_settextencoding"的情况下处理多字节语言。但不管怎样,问题已经过去了。我很高兴!!

最新更新