简体   繁体   English

Avmetadataitem从iOS上的iTunes或ID3元数据获取trackNumber

[英]Avmetadataitem Getting the trackNumber from an iTunes or ID3 metadata on iOS

I'm trying to get the trackNumber of a aac, mp3 or mp4 file. 我正在尝试获取aac,mp3或mp4文件的trackNumber。 It's not in the commonMetadata so I started to spelunk in the other metadata keys. 它不在commonMetadata中,所以我开始对其他元数据键进行摸索。 I found something that looks like it, but I'm yet unable to read it, and make sense of it. 我发现了一些看起来像它的东西,但我仍无法阅读并理解它。 Even the raw data makes no sense to me. 即使是原始数据,对我来说也毫无意义。

At the moment, I'm just trying to get it using this basic code: 目前,我只是尝试使用以下基本代码来获取它:

    NSArray *meta = [asset metadataForFormat:AVMetadataFormatiTunesMetadata];
    for ( AVMetadataItem* item in meta ) {
        id key = [item key];
        NSString *value = [item stringValue];
        NSLog(@"key = %@, value = %@", key, value);
    }

Knowing I'm looking for AVMetadataiTunesMetadataKeyTrackNumber. 知道我在寻找AVMetadataiTunesMetadataKeyTrackNumber。

I realize this thread is quite old but I recently came across this issue myself. 我知道这个线程已经很老了,但是最近我自己遇到了这个问题。 I needed ALL the metadata I could gather and came up with the following solution. 我需要我可以收集的所有元数据,并提出了以下解决方案。 It's not the most elegant solution but it works well enough. 这不是最优雅的解决方案,但效果很好。 Written in Swift. 用Swift编写。

func processFile(url:NSURL) {
    let yearKey = -1453039239
    let genreKey = -1452841618
    let encoderKey = -1451987089
    let trackKey = "com.apple.iTunes.iTunes_CDDB_TrackNumber"
    let CDDBKey = "com.apple.iTunes.iTunes_CDDB_1"
    let path:String = url.absoluteString
    let asset = AVURLAsset(URL: url, options: nil)
    let format = AVMetadataFormatiTunesMetadata


    for item:AVMetadataItem in asset.metadataForFormat(format) as Array<AVMetadataItem> {

        if let key = item.commonKey { if key == "title" { println(item.value()) } }
        if let key = item.commonKey { if key == "artist" { println(item.value()) } }
        if let key = item.commonKey { if key == "albumName" { println(item.value()) } }
        if let key = item.commonKey { if key == "creationDate" { println(item.value()) } }
        if let key = item.commonKey { if key == "artwork" { println( "art" ) } }

        if item.key().isKindOfClass(NSNumber) {
            if item.key() as NSNumber == yearKey { println("year: \(item.numberValue)") }
            if item.key() as NSNumber == genreKey { println("genre: \(item.stringValue)") }
            if item.key() as NSNumber == encoderKey { println("encoder: \(item.stringValue)") }
        }

        if item.key().isKindOfClass(NSString) {
            if item.key() as String == trackKey { println("track: \(item.stringValue)") }
            if item.key() as String == CDDBKey { println("CDDB: \(item.stringValue)") }
        }
    }
}

If your track has ID3 metadata, you can easily get the numberValue for the track number. 如果您的轨道具有ID3元数据,则可以轻松获取轨道号的numberValue。 If your track has iTunesMetadata, the dataValue is all you get. 如果您的曲目中包含iTunesMetadata,则您将获得dataValue。 You have to guess the intValue yourself. 您必须自己猜测intValue。

So far, I'm here. 到目前为止,我在这里。 I'm pretty sure I need to work more on the bytes portion. 我很确定我需要在字节部分上做更多的工作。

    NSArray *meta = [asset metadataForFormat:AVMetadataFormatiTunesMetadata];
    NSArray *itfilteredKeys = [AVMetadataItem metadataItemsFromArray:meta withKey:AVMetadataiTunesMetadataKeyTrackNumber keySpace:nil];
    for ( AVMetadataItem* item in itfilteredKeys ) {
        NSData *value = [item dataValue];
        unsigned char aBuffer[4];
        [value getBytes:aBuffer length:4];
        unsigned char *n = [value bytes];
        int value1 = aBuffer[3];
        NSLog(@"trackNumber from iTunes = %i", value1);
    }

This question is rather old, but I came upon it as I had a similar problem, so for anyone who still needs a solution. 这个问题相当古老,但是我遇到了类似的问题,所以对于任何仍需要解决方案的人来说,我都会遇到这个问题。 I managed to figure out a way of getting the total tracks number and the track number using the following code: 我设法找到一种使用以下代码获取总曲目号和曲目号的方法:

NSString *value = @"<0000000a 00140000>" //[item stringValue] (in this case 10/20)
NSString *track_no_hex = [[value componentsSeparatedByString:@" "][0] stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSString *total_track_no_hex = [[value componentsSeparatedByString:@" "][1] stringByReplacingOccurrencesOfString:@">" withString:@""];

NSString *track_no = @"";
for(int k=0;k<=4;k+=4){
    unsigned result = 0;
    NSScanner *scanner = [NSScanner scannerWithString:[track_no_hex substringWithRange:NSMakeRange(k, 4)]];
    [scanner scanHexInt:&result];
    if(result == 00){

    }
    else{
         track_no = [NSString stringWithFormat:@"%@%u",track_no,result];
    }
}

NSString *total_track_no = @"";
for(int k=0;k<=4;k+=4){
    unsigned result = 0;
    NSScanner *scanner;
    if(k+4<=[total_track_no_hex length]){
        scanner = [NSScanner scannerWithString:[total_track_no_hex substringWithRange:NSMakeRange(k, 4)]];
    }
    else{

    }
    [scanner scanHexInt:&result];
    if(result == 00){

    }
    else{
       total_track_no = [NSString stringWithFormat:@"%@%u",total_track_no,result];
    }
}
NSLog(@"%@/%@",track_no, total_track_no); // Output 10/20

This will work fine for track numbers under 14461 which should be large enough considering iTunes max track number is 999. 对于14461以下的曲目编号,此方法将很好用,考虑到iTunes的最大曲目编号为999,应该足够大。

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

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