Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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