iOS - 获取音视频文件的Metadata信息

//

//  MusicInfoArray.h

//  LocationMusic

//

//  Created by Wengrp on 2017/6/22.

//  Copyright © 2017年 Wengrp. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

#import <AVFoundation/AVFoundation.h>

@interface MusicInfoArray : NSObject

/**

获取音视频文件的Metadata信息(可以获取到mp3以及m4a的相关信息)

*/

+ (NSMutableArray *)MusicInfoArray;

@end

//

//  MusicInfoArray.m

//  LocationMusic

//

//  Created by Wengrp on 2017/6/22.

//  Copyright © 2017年 Wengrp. All rights reserved.

//

#import "MusicInfoArray.h"

@implementation MusicInfoArray

+ (NSMutableArray *)MusicInfoArray

{

    NSMutableArray *resultArray = [[NSMutableArray alloc] init];

    NSArray *mp3Array = [NSBundle pathsForResourcesOfType:@"mp3" inDirectory:[[NSBundle mainBundle] pathForResource:@"Music" ofType:@"bundle"]];

    for (NSString *filePath in mp3Array) {

        NSURL *url = [NSURL fileURLWithPath:filePath];

        NSString *MusicName = [filePath lastPathComponent];

        AVURLAsset *mp3Asset = [AVURLAsset URLAssetWithURL:url options:nil];

        NSLog(@"%@",mp3Asset);

        for (NSString *format in [mp3Asset availableMetadataFormats]) {

            NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] init];

            [infoDict setObject:MusicName forKey:@"MusicName"];

            NSLog(@"format type = %@",format);

            for (AVMetadataItem *metadataItem in [mp3Asset metadataForFormat:format]) {

                NSLog(@"commonKey = %@",metadataItem.commonKey);

                if ([metadataItem.commonKey isEqualToString:@"artwork"]) {

                    NSString *mime = [(NSDictionary *)metadataItem.value objectForKey:@"MIME"];

                    NSLog(@"mime: %@",mime);

                    [infoDict setObject:mime forKey:@"artwork"];

                }

                else if([metadataItem.commonKey isEqualToString:@"title"])

                {

                    NSString *title = (NSString *)metadataItem.value;

                    NSLog(@"title: %@",title);

                    [infoDict setObject:title forKey:@"title"];

                }

                else if([metadataItem.commonKey isEqualToString:@"artist"])

                {

                    NSString *artist = (NSString *)metadataItem.value;

                    NSLog(@"artist: %@",artist);

                    [infoDict setObject:artist forKey:@"artist"];

                }

                else if([metadataItem.commonKey isEqualToString:@"albumName"])

                {

                    NSString *albumName = (NSString *)metadataItem.value;

                    NSLog(@"albumName: %@",albumName);

                    [infoDict setObject:albumName forKey:@"albumName"];

                }

            }

            [resultArray addObject:infoDict];

        }

    }

    return resultArray;

}

@end
上一篇:tribonacci


下一篇:iBatisSQL中prepend的问题