Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95 - (hide annotations) (download)
Sun Nov 22 04:46:20 2009 UTC (14 years, 4 months ago) by masaki
File size: 5071 byte(s)
[Mod] XspfMMainThreadRequestの遅延時間をサブクラスで変更可能にした。
1 masaki 2 //
2     // XspfMMovieLoadRequest.m
3     // XspfManager
4     //
5     // Created by Hori,Masaki on 09/11/01.
6     // Copyright 2009 masakih. All rights reserved.
7     //
8    
9     #import "XspfMMovieLoadRequest.h"
10    
11     #import <QTKit/QTKit.h>
12     #import "XspfQTComponent.h"
13     #import "XspfQTValueTransformers.h"
14    
15    
16     @implementation XspfMMovieLoadRequest
17    
18     @synthesize object;
19     @synthesize url;
20    
21 masaki 71 + (id)requestWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl;
22 masaki 2 {
23     return [[[self alloc] initWithObject:anObject url:anUrl] autorelease];
24     }
25 masaki 71 - (id)initWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl
26 masaki 2 {
27     self = [super init];
28     self.object = anObject;
29     self.url = anUrl;
30    
31     return self;
32     }
33 masaki 89 - (void)delloc
34     {
35     self.object = nil;
36     self.url = nil;
37    
38     [super dealloc];
39     }
40 masaki 95 - (NSTimeInterval)sleepTime
41     {
42     return 1.0;
43     }
44 masaki 2
45     static QTMovie *loadFromMovieURL(NSURL *url)
46     {
47 masaki 88 NSError *error = nil;
48 masaki 2
49 masaki 88 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
50     url, QTMovieURLAttribute,
51     [NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute,
52     nil];
53     QTMovie *result = [[QTMovie alloc] initWithAttributes:attrs error:&error];
54     if (result == nil) {
55     if (error != nil) {
56     NSLog(@"Couldn't load movie URL, error = %@", error);
57     }
58     }
59 masaki 2
60 masaki 88 return result;
61 masaki 2 }
62    
63     static XspfQTComponent *componentForURL(NSURL *url)
64     {
65     NSError *theErr = nil;
66    
67     NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url
68     options:0
69     error:&theErr] autorelease];
70     if(!d) {
71     if(theErr) {
72     NSLog(@"%@", theErr);
73     }
74     return nil;
75     }
76     NSXMLElement *root = [d rootElement];
77     XspfQTComponent *pl = [XspfQTComponent xspfComponemtWithXMLElement:root];
78     if(!pl) {
79     NSLog(@"Can not create XspfQTComponent.");
80     return nil;
81     }
82    
83     return pl;
84     }
85    
86     static inline NSSize maxSizeForFrame(NSSize size, CGSize frame)
87     {
88     NSSize result = size;
89     CGFloat aspectRetio = size.width / size.height;
90     CGFloat frameAspectRetio = frame.width / frame.height;
91    
92     if(aspectRetio > frameAspectRetio) {
93     result.width = frame.width;
94     result.height = result.width / aspectRetio;
95     } else {
96     result.height = frame.height;
97     result.width = result.height * aspectRetio;
98     }
99    
100     return result;
101     }
102    
103     /** ���������������������������������������������������������������������������������������������������������������������������������������������������
104     ** ������������������������������������������������������������
105     ** ���������������������������������������������������������������������
106     ** ������������������������������������������������������������
107     ** ���������������������������������������������������������������������������
108     **/
109     static QTTime calcDefaultThumbnailQTTime(QTMovie *movie)
110     {
111     XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease];
112    
113     NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute];
114     id pV = [t transformedValue:pTimeValue];
115     if([pV longValue] == 0) {
116     NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute];
117     id v = [t transformedValue:duration];
118    
119     double newPosterTime = 0;
120     double dDur = [v doubleValue];
121     if(dDur > 15 * 60) {
122     newPosterTime = dDur / 100;
123     } else if(dDur > 60) {
124     newPosterTime = 1;
125     }
126     pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]];
127     }
128    
129     return [pTimeValue QTTimeValue];
130     }
131     static NSImage *thumbnailForTrackTime(XspfQTComponent *track, NSTimeInterval time, CGSize size)
132     {
133     NSError *theErr = nil;
134     QTMovie *movie = loadFromMovieURL([track movieLocation]);
135    
136     NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute];
137     NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size);
138    
139     QTTime t;
140     if(time == DBL_MIN) {
141     t = calcDefaultThumbnailQTTime(movie);
142     } else {
143     t = QTMakeTimeWithTimeInterval(time);
144     }
145    
146     NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys:
147     QTMovieFrameImageTypeNSImage,QTMovieFrameImageType,
148     [NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize,
149     nil];
150    
151     NSImage *theImage = (NSImage *)[movie frameImageAtTime:t
152     withAttributes:imgProp
153     error:&theErr];
154     if (theImage == nil) {
155     if (theErr != nil) {
156     NSLog(@"Couldn't create CGImageRef, error = %@", theErr);
157     }
158     return NULL;
159     }
160    
161     return theImage;
162     }
163    
164     static NSImage *thumbnailWithComponent(XspfQTComponent *component)
165     {
166     XspfQTComponent *track = [component thumbnailTrack];
167     NSTimeInterval interval = [component thumbnailTimeInterval];
168     CGSize size = { 200, 200 };
169    
170     if(!track) {
171     XspfQTComponent *trackList = [component childAtIndex:0];
172     [trackList setSelectionIndex:0];
173     track = [trackList currentTrack ];
174     }
175    
176     NSImage *thumbnail = thumbnailForTrackTime(track, interval, size);
177    
178     return thumbnail;
179     }
180    
181    
182 masaki 85 - (void)fire
183 masaki 2 {
184     id item = componentForURL(self.url);
185     if(!item) return;
186 masaki 7 if([item childrenCount] == 0) return;
187 masaki 2
188 masaki 7 id trackList = [item childAtIndex:0];
189 masaki 71 self.object.movieNum = [NSNumber numberWithInt:[trackList childrenCount]];
190     self.object.thumbnail = thumbnailWithComponent(item);
191 masaki 2 }
192     -(void)terminate
193     {
194     [self doesNotRecognizeSelector:_cmd];
195     }
196     @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26