-
Notifications
You must be signed in to change notification settings - Fork 3
/
NSURL+TagLib.m
149 lines (132 loc) · 3.89 KB
/
NSURL+TagLib.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// NSURL+TagLib.m
// Loopseque Twins
//
// Created by Павел Литвиненко on 25.09.14.
// Copyright (c) 2014 Casual Underground. All rights reserved.
//
//_______________________________________________________________________________________________________________
#import "NSURL+TagLib.h"
#import "TagLib+CoverArt.h"
#import "tag_c.h"
//_______________________________________________________________________________________________________________
@implementation NSURL (TagLib)
@dynamic artwork;
@dynamic artistName;
@dynamic trackTitle;
@dynamic trackLength;
//_______________________________________________________________________________________________________________
#pragma mark - Getters
- (NSString*)artistName
{
TagLib_File *file = taglib_file_new([self.path UTF8String]);
NSString *str = nil;
// Check opened file
if (file && taglib_file_is_valid(file))
{
TagLib_Tag *tag = taglib_file_tag(file);
char *val = taglib_tag_artist(tag);
str = [NSString stringWithUTF8String:val];
// Free taglib string
taglib_free(val);
}
// Close file
taglib_file_free(file);
// Return string
return str;
}
- (NSString*)trackTitle
{
TagLib_File *file = taglib_file_new([self.path UTF8String]);
NSString *str = nil;
// Check opened file
if (file && taglib_file_is_valid(file))
{
TagLib_Tag *tag = taglib_file_tag(file);
char *val = taglib_tag_title(tag);
str = [NSString stringWithUTF8String:val];
// Free taglib string
taglib_free(val);
}
// Close file
taglib_file_free(file);
// Return string
return str;
}
- (UIImage*)artwork
{
CFStringRef _path = (__bridge CFStringRef)(self.path);
CFDataRef _data = NULL;
UIImage *_img = nil;
// Get data from file
if (!CopyCoverArtDataFromFileAtPath(_path, &_data))
{
goto exit;
}
exit:
// Init image with data and release fata
if (_data)
{
_img = [UIImage imageWithData:(__bridge NSData *)(_data)];
CFRelease(_data);
}
// Return image
return _img;
}
- (NSUInteger)trackLength
{
TagLib_File *file = taglib_file_new([self.path UTF8String]);
NSUInteger length = 0;
// Check opened file
if (file && taglib_file_is_valid(file))
{
const TagLib_AudioProperties *props = taglib_file_audioproperties(file);
if (props)
{
length = taglib_audioproperties_length(props);
}
}
// Close file
taglib_file_free(file);
return length;
}
//_______________________________________________________________________________________________________________
#pragma mark - Setters
- (void)setArtistName:(NSString*)artist
{
TagLib_File *file = taglib_file_new([self.path UTF8String]);
// Check opened file
if (file && taglib_file_is_valid(file))
{
TagLib_Tag *tag = taglib_file_tag(file);
taglib_tag_set_artist(tag, [artist UTF8String]);
}
// Close file
taglib_file_save(file);
taglib_file_free(file);
}
- (void)setTrackTitle:(NSString*)title
{
TagLib_File *file = taglib_file_new([self.path UTF8String]);
// Check opened file
if (file && taglib_file_is_valid(file))
{
TagLib_Tag *tag = taglib_file_tag(file);
taglib_tag_set_title(tag, [title UTF8String]);
}
// Close file
taglib_file_save(file);
taglib_file_free(file);
}
- (void)setArtwork:(UIImage*)artwork
{
CFStringRef _path = (__bridge CFStringRef)(self.path);
CFDataRef _data = (__bridge CFDataRef)(UIImageJPEGRepresentation(artwork, 1.0));
// Set artwork
if (!SetCoverArtDataToFileAtPath(_path, _data))
{
NSLog(@"Failed to write cover art image for file at path: %@", self.relativePath);
}
}
@end
//_______________________________________________________________________________________________________________