简体   繁体   English

如何为AVMetadataFormatiTunesMetadata创建AVMutableMetadataItem

[英]How do you create an AVMutableMetadataItem for AVMetadataFormatiTunesMetadata

I am trying to update the track number of files using AVMutableMetadataItem's. 我正在尝试使用AVMutableMetadataItem更新文件的轨道数。 I have been having some success except for AVMetadataFormatiTunesMetadata. 除了AVMetadataFormatiTunesMetadata之外,我一直在取得一些成功。 I cannot seem to figure out what I need to set as the value to get this to work. 我似乎无法弄清楚我需要设置什么作为使其工作的值。 I have tried to create an archive of a NSArray as follows 我试图创建一个NSArray的存档,如下所示

[NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:
    [NSNumber numberWithUnsignedInt:13],
    [NSNumber numberWithUnsignedInt:32],
    nil]];

But I end up with a track and trackOf numbers that are way off. 但我最终得到了一个跟踪和追踪的数字。 What exactly am I supposed to be passing in as the value for an AVMutableMetadataItem with a key of AVMetadataiTunesMetadataKeyTrackNumber? 我应该准确传递一个带有AVMetadataiTunesMetadataKeyTrackNumber键的AVMutableMetadataItem的值?

It took me a very long time to figure that one out. 我花了很长时间才弄明白这一点。 Thanks to Apple's Core Audio documentation, which says absolutely nothing about how to deal with it. 感谢Apple的Core Audio文档,该文档完全没有说明如何处理它。 Or any of the other keys for that matter. 或任何其他关键的事情。 I had to examine an MP4 file with track information before I understood. 在我理解之前,我不得不检查带有轨道信息的MP4文件。

Answer 回答

You need to assign it with an NSData containing the track information. 您需要为其分配包含轨道信息的NSData。

The data must consists of four 16-bit big endian values, whereas the 2nd is the track number and the 3rd is the total tracks in collection . 数据必须由四个16位大端值组成,而第二个是轨道号 ,第三个是集合中总轨道数 1st and 4th should be zero. 第1和第4应为零。

So basically you need to do this 所以基本上你需要这样做

int16_t trackNumber = 1; // track number
int16_t tracksInCollection = 12; // total number of tracks in collection
int16_t data[4] = { 0, trackNumber, tracksInCollection, 0 };

metadataItem.keySpace = AVMetadataKeySpaceiTunes;
metadataItem.key = AVMetadataiTunesMetadataKeyTrackNumber;
metadataItem.value = [NSData dataWithBytes:data length:sizeof(data)];

Notice: The same approach is applied for the AVMetadataiTunesMetadataKeyDiscNumber key. 注意:AVMetadataiTunesMetadataKeyDiscNumber密钥应用相同的方法。

A remarks on endianness 关于字节序的评论

If you don't want to worry about byte-order, you can "borrow" a methods from the Berkeley sockets API. 如果您不想担心字节顺序,可以从Berkeley套接字API“借用”一个方法。 Or it might be a macro. 或者它可能是一个宏。 Anyhow, it works like this: 无论如何,它的工作原理如下:

bigendianval = htons(val);

or 要么

int16_t trackNumber = htons(myTrackNumberVariable);

htons ( Host to network short ) will convert your 16-bit numbers to big endian - regardless of endianness of your own system. htons( 主机到网络短 )将您的16位数字转换为大端 - 无论您自己的系统的字节顺序如何。 IP-networks are also big-endian, and therefore htons is reusable here. IP网络也是big-endian,因此htons在这里是可重用的。

Similarly, for disk number: 同样,对于磁盘编号:

AVMutableMetadataItem *metaItem = [AVMutableMetadataItem metadataItem];
metaItem.key=@1684632427;

int16_t diskNumber = htons([dict[@"discNumber"]intValue]);
int16_t disksInCollection = htons([dict[@"discCount"]intValue]);
int16_t data[3] = {0, diskNumber, disksInCollection};
metaItem.value=[NSData dataWithBytes:data length:sizeof(data)];
metaItem.keySpace=AVMetadataKeySpaceiTunes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM