• R/O
  • SSH
  • HTTPS

xspfqt: Commit


Commit MetaInfo

Révision385 (tree)
l'heure2013-06-23 17:58:07
Auteurmasakih

Message de Log

[Refactoring] XspfQTDocumentの再生関連をXspfQTPlayerクラスに分割

Change Summary

Modification

--- branches/divideMovieViewController/XspfQTDocument.m (revision 384)
+++ branches/divideMovieViewController/XspfQTDocument.m (revision 385)
@@ -67,13 +67,12 @@
6767 #import "XspfQTPlayListWindowController.h"
6868 #import <QTKit/QTKit.h>
6969
70-#import "NSURL-HMExtensions.h"
71-#import "XspfQTMovieLoader.h"
7270 #import "XspfQTValueTransformers.h"
7371
74-#import "XspfQTMovieTimer.h"
72+#import "XspfQTPlayer.h"
7573
7674
75+
7776 #pragma mark #### Global Variables ####
7877 /********* Global variables *******/
7978 NSString *XspfQTDocumentWillCloseNotification = @"XspfQTDocumentWillCloseNotification";
@@ -82,13 +81,10 @@
8281
8382 @interface XspfQTDocument()
8483 @property (retain) HMXSPFComponent *playlist;
85-@property (retain) QTMovie *playingMovie;
8684 @property (readonly) NSXMLDocument *XMLDocument;
8785
88-// private
89-@property (retain) XspfQTMovieLoader *loader;
90-@property NSTimeInterval playingMovieDuration;
91-@property BOOL didPreloading;
86+@property (retain) XspfQTPlayer *player;
87+
9288 @end
9389
9490 @interface XspfQTDocument (Private)
@@ -105,27 +101,16 @@
105101
106102 static NSString *XspfUTI = @"com.masakih.xspf";
107103
108-static NSString *XspfQTCurrentTrackKey = @"currentTrack";
109-
110104 @implementation XspfQTDocument
111-@synthesize playlist = _playlist;
112-@synthesize playingMovie = _playingMovie;
113105
114-@synthesize loader = _loader;
115-@synthesize playingMovieDuration = _playingMovieDuration;
116-@synthesize didPreloading = _didPreloading;
106+@synthesize player = _player;
117107
118-static XspfQTMovieTimer* timer = nil;
119-+ (void)initialize
120-{
121- timer = [[XspfQTMovieTimer movieTimer] retain];
122-}
123108
124109 - (id)init
125110 {
126111 self = [super init];
127112 if(self) {
128- self.loader = [[XspfQTMovieLoader loaderWithMovieURL:nil] retain];
113+ _player = [[XspfQTPlayer alloc] init];
129114 }
130115
131116 return self;
@@ -146,12 +131,12 @@
146131 }
147132 - (void)dealloc
148133 {
149- self.playingMovie = nil;
150134 self.playlist = nil;
151135 [playListWindowController release];
152136 [movieWindowController release];
153- self.loader = nil;
154137
138+ [_player release];
139+
155140 [super dealloc];
156141 }
157142
@@ -165,8 +150,6 @@
165150 [self addWindowController:movieWindowController];
166151
167152 [movieWindowController showWindow:nil];
168-
169- [timer put:self];
170153 }
171154
172155 - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
@@ -316,96 +299,24 @@
316299
317300 - (void)setPlaylist:(HMXSPFComponent *)newList
318301 {
319- if(_playlist == newList) return;
320-
321- [[_playlist childAtIndex:0] removeObserver:self forKeyPath:XspfQTCurrentTrackKey];
322- [_playlist autorelease];
323- _playlist = [newList retain];
324- [[_playlist childAtIndex:0] addObserver:self
325- forKeyPath:XspfQTCurrentTrackKey
326- options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
327- context:NULL];
302+ self.player.playlist = newList;
328303 }
329304 - (HMXSPFComponent *)playlist
330305 {
331- return _playlist;
306+ return self.player.playlist;
332307 }
333-
334308 - (HMXSPFComponent *)trackList
335309 {
336- return [self.playlist childAtIndex:0];
310+ return self.player.trackList;
337311 }
338-
339-+ (NSSet *)keyPathsForValuesAffectingPlayingMovieDuration
340-{
341- return [NSSet setWithObject:@"playingMovie"];
342-}
343-- (void)setPlayingMovie:(QTMovie *)newMovie
344-{
345- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
346- if(_playingMovie) {
347- [nc removeObserver:self
348- name:nil
349- object:_playingMovie];
350- }
351-
352- [_playingMovie autorelease];
353- _playingMovie = [newMovie retain];
354- self.playingMovieDuration = 0;
355-
356- if(_playingMovie) {
357- [nc addObserver:self
358- selector:@selector(notifee:)
359- name:QTMovieRateDidChangeNotification
360- object:_playingMovie];
361- }
362-}
363312 - (QTMovie *)playingMovie
364313 {
365- return _playingMovie;
314+ return self.player.playingMovie;
366315 }
367-- (void)setPlayingMovieDuration:(NSTimeInterval)playingMovieDuration
368-{
369- _playingMovieDuration = playingMovieDuration;
370-}
371-- (NSTimeInterval)playingMovieDuration
372-{
373- if(_playingMovieDuration == 0) {
374- QTTime qttime = [self.playingMovie duration];
375- if(!QTGetTimeInterval(qttime, &_playingMovieDuration)) _playingMovieDuration = 0;
376- }
377-
378- return _playingMovieDuration;
379-}
380-- (void)loadMovie
381-{
382- NSURL *location = self.trackList.movieLocation;
383-
384- if(self.playingMovie) {
385- id movieURL = [self.playingMovie attributeForKey:QTMovieURLAttribute];
386- if([location isEqualUsingLocalhost:movieURL]) return;
387- }
388-
389- self.loader.movieURL = location;
390- [self.loader load];
391- QTMovie *newMovie = self.loader.qtMovie;
392- self.playingMovie = newMovie;
393-
394- QTTime qttime = [newMovie duration];
395- id t = [NSValueTransformer valueTransformerForName:@"XspfQTTimeDateTransformer"];
396- [self.trackList setCurrentTrackDuration:[t transformedValue:[NSValue valueWithQTTime:qttime]]];
397-
398- self.didPreloading = NO;
399-}
400316
401-- (void)observeValueForKeyPath:(NSString *)keyPath
402- ofObject:(id)object
403- change:(NSDictionary *)change
404- context:(void *)context
317++ (NSSet *)keyPathsForValuesAffectingPlayingMovie
405318 {
406- if([keyPath isEqualToString:XspfQTCurrentTrackKey]) {
407- [self loadMovie];
408- }
319+ return [NSSet setWithObject:@"player.playingMovie"];
409320 }
410321
411322 - (NSXMLDocument *)XMLDocument
@@ -480,49 +391,7 @@
480391 return data;
481392 }
482393
483-- (void)notifee:(id)notification
484-{
485- // NSLog(@"Notifed: name -> (%@)\ndict -> (%@)", [notification name], [notification userInfo]);
486-
487- HMXSPFComponent *track = self.trackList.currentTrack;
488- NSNumber *rateValue = [[notification userInfo] objectForKey:QTMovieRateDidChangeNotificationParameter];
489- if(rateValue) {
490- float rate = [rateValue floatValue];
491- if(rate == 0) {
492- track.isPlayed = NO;
493- } else {
494- track.isPlayed = YES;
495- }
496- }
497-}
498394
499-// call from XspfQTMovieTimer.
500-- (void)checkPreload:(NSTimer *)timer
501-{
502- if(![XspfQTPref preloadingEnabled]) return;
503- if(self.didPreloading) return;
504-
505- NSTimeInterval duration;
506- NSTimeInterval current;
507- QTTime qttime = [self.playingMovie currentTime];
508- if(!QTGetTimeInterval(qttime, &current)) return;
509-
510- duration = self.playingMovieDuration;
511-
512- if( current / duration > XspfQTPref.beginingPreloadPercent ) {
513- self.didPreloading = YES;
514- HMXSPFComponent *list = self.trackList;
515- NSUInteger nextIndex = list.selectionIndex + 1;
516- NSUInteger max = list.childrenCount;
517- if(max <= nextIndex) return;
518-
519- HMXSPFComponent *nextTrack = [list childAtIndex:nextIndex];
520- NSURL *nextMovieURL = nextTrack.movieLocation;
521- self.loader.movieURL = nextMovieURL;
522- [self.loader load];
523- }
524-}
525-
526395 inline static BOOL isXspfFileType(NSString *typeName)
527396 {
528397 return [typeName isEqualToString:XspfDocumentType] || [typeName isEqualToString:XspfUTI];
--- branches/divideMovieViewController/XspfQTMovieTimer.m (revision 384)
+++ branches/divideMovieViewController/XspfQTMovieTimer.m (revision 385)
@@ -69,23 +69,8 @@
6969 self = [super init];
7070
7171 documents = [[NSMutableArray alloc] init];
72- movieWindowControllers = [[NSMutableDictionary alloc] init];
72+ pausedDocuments = [[NSMutableArray alloc] init];
7373
74- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
75- [nc addObserver:self
76- selector:@selector(documentWillClose:)
77- name:XspfQTDocumentWillCloseNotification
78- object:nil];
79-
80- [nc addObserver:self
81- selector:@selector(movieDidStart:)
82- name:XspfQTMovieDidStartNotification
83- object:nil];
84- [nc addObserver:self
85- selector:@selector(movieDidPause:)
86- name:XspfQTMovieDidPauseNotification
87- object:nil];
88-
8974 return self;
9075 }
9176
@@ -96,7 +81,7 @@
9681 - (void)dealloc
9782 {
9883 [documents release];
99- [movieWindowControllers release];
84+ [pausedDocuments release];
10085 [timer invalidate]; timer = nil;
10186
10287 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
@@ -109,7 +94,7 @@
10994 {
11095 @synchronized(self) {
11196 if(timer) return;
112- timer = [NSTimer scheduledTimerWithTimeInterval:0.5
97+ timer = [NSTimer scheduledTimerWithTimeInterval:0.1
11398 target:self
11499 selector:@selector(fire:)
115100 userInfo:NULL
@@ -161,13 +146,6 @@
161146 if([pausedDocuments containsObject:doc]) return;
162147
163148 [documents addObject:doc];
164-
165- NSArray *wControlers = [doc windowControllers];
166- for(id w in wControlers) {
167- if([w isKindOfClass:[XspfQTMovieWindowController class]]) {
168- [movieWindowControllers setObject:w forKey:[NSValue valueWithPointer:doc]];
169- }
170- }
171149 }
172150
173151 [self makeTimer];
@@ -175,8 +153,6 @@
175153 - (void)removeDocument:(XspfQTDocument *)doc
176154 {
177155 @synchronized(documents) {
178- [movieWindowControllers removeObjectForKey:[NSValue valueWithPointer:doc]];
179-
180156 if([documents containsObject:doc]) {
181157 [documents removeObject:doc];
182158 }
@@ -190,41 +166,31 @@
190166 }
191167 }
192168
193-- (void)put:(XspfQTDocument *)doc
169+- (void)put:(id)doc
194170 {
195171 [self addDocument:doc];
196172 }
197-
198-- (void)documentWillClose:(id)notification
173+- (void)remove:(id)doc
199174 {
200- id doc = [notification object];
201175 [self removeDocument:doc];
202176 }
203-
204-- (void)movieDidStart:(id)notification
177+- (void)suspend:(id)doc
205178 {
206- id wc = [notification object];
207- XspfQTDocument *doc = [wc document];
208- [self enableFireing:doc];
179+ [self disableFireing:doc];
209180 }
210-- (void)movieDidPause:(id)notification
181+- (void)resume:(id)doc
211182 {
212- id wc = [notification object];
213- XspfQTDocument *doc = [wc document];
214- [self disableFireing:doc];
183+ [self enableFireing:doc];
215184 }
216185
186+
217187 - (void)fire:(id)t
218188 {
219189 XspfQTDocument *doc;
220- XspfQTMovieWindowController *wc;
221190
222191 @synchronized(documents) {
223- for(doc in documents) {
224- wc = [movieWindowControllers objectForKey:[NSValue valueWithPointer:doc]];
225-
192+ for(doc in documents) {
226193 [doc checkPreload:t];
227- [wc updateTimeIfNeeded:t];
228194 }
229195 }
230196 }
--- branches/divideMovieViewController/XspfQTMovieWindowController.m (revision 384)
+++ branches/divideMovieViewController/XspfQTMovieWindowController.m (revision 385)
@@ -580,14 +580,15 @@
580580 }
581581
582582 // call from XspfQTMovieTimer.
583+// TODO: no longer call method. other way for hide cursor.
583584 - (void)updateTimeIfNeeded:(id)timer
584585 {
585- QTMovie *qt = self.qtMovie;
586- if(qt) {
587- // force update time indicator.
588- [qt willChangeValueForKey:@"currentTime"];
589- [qt didChangeValueForKey:@"currentTime"];
590- }
586+// QTMovie *qt = self.qtMovie;
587+// if(qt) {
588+// // force update time indicator.
589+// [qt willChangeValueForKey:@"currentTime"];
590+// [qt didChangeValueForKey:@"currentTime"];
591+// }
591592
592593 // Hide cursor and controller, if mouse didn't move for 3 seconds.
593594 NSPoint mouse = [NSEvent mouseLocation];
--- branches/divideMovieViewController/XspfQTMovieViewController.m (revision 384)
+++ branches/divideMovieViewController/XspfQTMovieViewController.m (revision 385)
@@ -31,7 +31,13 @@
3131 return self;
3232 }
3333
34+- (void)dealloc
35+{
36+ [self pause:nil];
37+ [super dealloc];
38+}
3439
40+
3541 - (QTMovieView *)movieView
3642 {
3743 return (QTMovieView *)[self view];
--- branches/divideMovieViewController/XspfQTPlayer.h (nonexistent)
+++ branches/divideMovieViewController/XspfQTPlayer.h (revision 385)
@@ -0,0 +1,29 @@
1+//
2+// XspfQTPlayer.h
3+// XspfQT
4+//
5+// Created by Hori,Masaki on 2013/06/21.
6+// Copyright (c) 2013年 masakih. All rights reserved.
7+//
8+
9+#import <Foundation/Foundation.h>
10+
11+@class HMXSPFComponent;
12+@class QTMovie;
13+@class XspfQTMovieLoader;
14+
15+@interface XspfQTPlayer : NSObject
16+{
17+ HMXSPFComponent *_playlist;
18+ QTMovie *_playingMovie;
19+ NSTimeInterval _playingMovieDuration;
20+
21+ XspfQTMovieLoader *_loader;
22+ BOOL _didPreloading;
23+}
24+
25+@property (retain) HMXSPFComponent *playlist;
26+@property (readonly) HMXSPFComponent *trackList;
27+@property (readonly, retain) QTMovie *playingMovie;
28+
29+@end
--- branches/divideMovieViewController/XspfQTPlayer.m (nonexistent)
+++ branches/divideMovieViewController/XspfQTPlayer.m (revision 385)
@@ -0,0 +1,218 @@
1+//
2+// XspfQTPlayer.m
3+// XspfQT
4+//
5+// Created by Hori,Masaki on 2013/06/21.
6+// Copyright (c) 2013年 masakih. All rights reserved.
7+//
8+
9+#import "XspfQTPlayer.h"
10+
11+#import <QTKit/QTKit.h>
12+
13+#import "HMXSPFComponent.h"
14+#import "NSURL-HMExtensions.h"
15+#import "XspfQTMovieLoader.h"
16+#import "XspfQTMovieTimer.h"
17+#import "XspfQTPreference.h"
18+
19+
20+
21+
22+static NSString *XspfQTCurrentTrackKey = @"currentTrack";
23+
24+
25+@interface XspfQTPlayer ()
26+
27+@property (retain) XspfQTMovieLoader *loader;
28+@property BOOL didPreloading;
29+
30+@end
31+
32+@implementation XspfQTPlayer
33+@synthesize loader = _loader;
34+@synthesize didPreloading = _didPreloading;
35+
36+
37+static XspfQTMovieTimer* timer = nil;
38++ (void)initialize
39+{
40+ static BOOL isFirst = YES;
41+ if(isFirst) {
42+ isFirst = NO;
43+ timer = [[XspfQTMovieTimer movieTimer] retain];
44+ }
45+}
46+
47+- (id)init
48+{
49+ self = [super init];
50+ if(self) {
51+ _loader = [[XspfQTMovieLoader loaderWithMovieURL:nil] retain];
52+ [timer put:self];
53+ }
54+
55+ return self;
56+}
57+
58+- (void)dealloc
59+{
60+ [_loader release];
61+ [timer remove:self];
62+
63+ [[_playlist childAtIndex:0] removeObserver:self forKeyPath:XspfQTCurrentTrackKey];
64+ [_playlist release];
65+
66+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
67+ [nc removeObserver:self];
68+ [_playingMovie release];
69+
70+ [super dealloc];
71+}
72+
73+- (void)setPlaylist:(HMXSPFComponent *)newList
74+{
75+ if(_playlist == newList) return;
76+
77+ [[_playlist childAtIndex:0] removeObserver:self forKeyPath:XspfQTCurrentTrackKey];
78+ [_playlist autorelease];
79+ _playlist = [newList retain];
80+ [[_playlist childAtIndex:0] addObserver:self
81+ forKeyPath:XspfQTCurrentTrackKey
82+ options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
83+ context:NULL];
84+
85+ if(!newList) {
86+ [timer remove:self];
87+ }
88+}
89+- (HMXSPFComponent *)playlist
90+{
91+ return _playlist;
92+}
93+
94+- (HMXSPFComponent *)trackList
95+{
96+ return [self.playlist childAtIndex:0];
97+}
98+
99++ (NSSet *)keyPathsForValuesAffectingPlayingMovieDuration
100+{
101+ return [NSSet setWithObject:@"playingMovie"];
102+}
103+- (void)setPlayingMovie:(QTMovie *)newMovie
104+{
105+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
106+ if(_playingMovie) {
107+ [nc removeObserver:self
108+ name:nil
109+ object:_playingMovie];
110+ }
111+
112+ [_playingMovie autorelease];
113+ _playingMovie = [newMovie retain];
114+ self.playingMovieDuration = 0;
115+
116+ if(_playingMovie) {
117+ [nc addObserver:self
118+ selector:@selector(notifee:)
119+ name:QTMovieRateDidChangeNotification
120+ object:_playingMovie];
121+ }
122+}
123+- (QTMovie *)playingMovie
124+{
125+ return _playingMovie;
126+}
127+- (void)setPlayingMovieDuration:(NSTimeInterval)playingMovieDuration
128+{
129+ _playingMovieDuration = playingMovieDuration;
130+}
131+- (NSTimeInterval)playingMovieDuration
132+{
133+ if(_playingMovieDuration == 0) {
134+ QTTime qttime = [self.playingMovie duration];
135+ if(!QTGetTimeInterval(qttime, &_playingMovieDuration)) _playingMovieDuration = 0;
136+ }
137+
138+ return _playingMovieDuration;
139+}
140+- (void)loadMovie
141+{
142+ NSURL *location = self.trackList.movieLocation;
143+
144+ if(self.playingMovie) {
145+ id movieURL = [self.playingMovie attributeForKey:QTMovieURLAttribute];
146+ if([location isEqualUsingLocalhost:movieURL]) return;
147+ }
148+
149+ self.loader.movieURL = location;
150+ [self.loader load];
151+ QTMovie *newMovie = self.loader.qtMovie;
152+ self.playingMovie = newMovie;
153+
154+ QTTime qttime = [newMovie duration];
155+ id t = [NSValueTransformer valueTransformerForName:@"XspfQTTimeDateTransformer"];
156+ [self.trackList setCurrentTrackDuration:[t transformedValue:[NSValue valueWithQTTime:qttime]]];
157+
158+ self.didPreloading = NO;
159+}
160+
161+- (void)observeValueForKeyPath:(NSString *)keyPath
162+ ofObject:(id)object
163+ change:(NSDictionary *)change
164+ context:(void *)context
165+{
166+ if([keyPath isEqualToString:XspfQTCurrentTrackKey]) {
167+ [self loadMovie];
168+ }
169+}
170+
171+
172+- (void)notifee:(id)notification
173+{
174+ HMXSPFComponent *track = self.trackList.currentTrack;
175+ NSNumber *rateValue = [[notification userInfo] objectForKey:QTMovieRateDidChangeNotificationParameter];
176+ if(rateValue) {
177+ float rate = [rateValue floatValue];
178+ if(rate == 0) {
179+ track.isPlayed = NO;
180+ [timer suspend:self];
181+ } else {
182+ track.isPlayed = YES;
183+ [timer resume:self];
184+ }
185+ }
186+}
187+
188+// call from XspfQTMovieTimer.
189+- (void)checkPreload:(NSTimer *)timer
190+{
191+ [self.playingMovie willChangeValueForKey:@"currentTime"];
192+ [self.playingMovie didChangeValueForKey:@"currentTime"];
193+
194+ if(![XspfQTPref preloadingEnabled]) return;
195+ if(self.didPreloading) return;
196+
197+ NSTimeInterval duration;
198+ NSTimeInterval current;
199+ QTTime qttime = [self.playingMovie currentTime];
200+ if(!QTGetTimeInterval(qttime, &current)) return;
201+
202+ duration = self.playingMovieDuration;
203+
204+ if( current / duration > XspfQTPref.beginingPreloadPercent ) {
205+ self.didPreloading = YES;
206+ HMXSPFComponent *list = self.trackList;
207+ NSUInteger nextIndex = list.selectionIndex + 1;
208+ NSUInteger max = list.childrenCount;
209+ if(max <= nextIndex) return;
210+
211+ HMXSPFComponent *nextTrack = [list childAtIndex:nextIndex];
212+ NSURL *nextMovieURL = nextTrack.movieLocation;
213+ self.loader.movieURL = nextMovieURL;
214+ [self.loader load];
215+ }
216+}
217+
218+@end
--- branches/divideMovieViewController/XspfQTDocument.h (revision 384)
+++ branches/divideMovieViewController/XspfQTDocument.h (revision 385)
@@ -67,24 +67,21 @@
6767 @class XspfQTMovieWindowController;
6868 @class XspfQTPlayListWindowController;
6969
70-@class XspfQTMovieLoader;
7170 @class QTMovie;
7271
72+@class XspfQTPlayer;
73+
7374 @interface XspfQTDocument : NSDocument
7475 {
7576 XspfQTMovieWindowController *movieWindowController;
7677 XspfQTPlayListWindowController *playListWindowController;
7778
78-#ifndef __LP64__
79- HMXSPFComponent* _playlist;
80- QTMovie *_playingMovie;
81- XspfQTMovieLoader *_loader;
82- NSTimeInterval _playingMovieDuration;
83- BOOL _didPreloading;
79+#ifndef __LP64__
80+ XspfQTPlayer *_player;
8481 #endif
8582 }
8683 @property (readonly) HMXSPFComponent *trackList;
87-@property (readonly, retain) QTMovie *playingMovie;
84+@property (readonly) QTMovie *playingMovie;
8885
8986
9087 - (IBAction)showPlayList:(id)sender;
--- branches/divideMovieViewController/XspfQTMovieTimer.h (revision 384)
+++ branches/divideMovieViewController/XspfQTMovieTimer.h (revision 385)
@@ -68,20 +68,22 @@
6868 @interface XspfQTMovieTimer : NSObject
6969 {
7070 NSTimer *timer;
71- NSMutableArray *documents;
72- NSMutableDictionary *movieWindowControllers;
73-
71+ NSMutableArray *documents;
7472 NSMutableArray *pausedDocuments;
7573 }
7674
7775 + (id)movieTimer;
7876
79-- (void)put:(XspfQTDocument *)doc;
77+- (void)put:(id)doc;
78+- (void)remove:(id)doc;
8079
80+- (void)suspend:(id)doc;
81+- (void)resume:(id)doc;
82+
8183 @end
8284
8385 @interface NSObject(XspfQTMovieTimer)
8486 - (void)checkPreload:(NSTimer *)timer;
85-- (void)updateTimeIfNeeded:(NSTimer *)timer;
87+//- (void)updateTimeIfNeeded:(NSTimer *)timer;
8688 @end
8789
Afficher sur ancien navigateur de dépôt.