CocoaでAVFoundationを使うサンプル
修訂 | 152e2d6f124296060c9e0d5fb122dbec3bd77c63 (tree) |
---|---|
時間 | 2012-06-11 21:41:30 |
作者 | Hori <masaki@MBP....> |
Commiter | Hori |
ソースコード整理
@@ -26,8 +26,6 @@ | ||
26 | 26 | @implementation HMDocument |
27 | 27 | @synthesize view = _view; |
28 | 28 | @synthesize playButton = _playButton; |
29 | -@synthesize timeGage = _timeGage; | |
30 | -@synthesize timeBar = _timeBar; | |
31 | 29 | @synthesize currentTime = _currentTime; |
32 | 30 | |
33 | 31 | @synthesize asset = _asset; |
@@ -38,6 +36,10 @@ | ||
38 | 36 | |
39 | 37 | - (void)dealloc |
40 | 38 | { |
39 | + [[NSNotificationCenter defaultCenter] removeObserver:self | |
40 | + name:AVPlayerItemDidPlayToEndTimeNotification | |
41 | + object:_item]; | |
42 | + | |
41 | 43 | [_player release]; |
42 | 44 | [_item release]; |
43 | 45 | [_asset release]; |
@@ -66,42 +68,6 @@ | ||
66 | 68 | { |
67 | 69 | self.asset = [AVURLAsset URLAssetWithURL:url options:nil]; |
68 | 70 | |
69 | - NSString *tracksKey = @"tracks"; | |
70 | - [_asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] | |
71 | - completionHandler: | |
72 | - ^ { | |
73 | - dispatch_async(dispatch_get_main_queue(), | |
74 | - ^{ | |
75 | - self.item = [AVPlayerItem playerItemWithAsset:_asset]; | |
76 | - [_item addObserver:self | |
77 | - forKeyPath:@"status" | |
78 | - options:0 | |
79 | - context:_item]; | |
80 | - [[NSNotificationCenter defaultCenter] addObserver:self | |
81 | - selector:@selector(playerItemDidReachEnd:) | |
82 | - name:AVPlayerItemDidPlayToEndTimeNotification | |
83 | - object:_item]; | |
84 | - | |
85 | - self.player = [AVPlayer playerWithPlayerItem:_item]; | |
86 | - [_player addObserver:self | |
87 | - forKeyPath:@"rate" | |
88 | - options:0 | |
89 | - context:_player]; | |
90 | - | |
91 | - [_player addPeriodicTimeObserverForInterval:CMTimeMake(4, 100) | |
92 | - queue:dispatch_get_main_queue() | |
93 | - usingBlock: ^(CMTime time) { | |
94 | - Float64 seconds = CMTimeGetSeconds(time); | |
95 | - self.currentTime = seconds; | |
96 | - }]; | |
97 | - | |
98 | - AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player]; | |
99 | - [_view setWantsLayer:YES]; | |
100 | - [_view setLayer:layer]; | |
101 | - }); | |
102 | - | |
103 | - }]; | |
104 | - | |
105 | 71 | return _asset ? YES : NO; |
106 | 72 | } |
107 | 73 |
@@ -131,13 +97,13 @@ | ||
131 | 97 | } |
132 | 98 | - (void)syncUI |
133 | 99 | { |
134 | - if((_player.currentItem != nil) && | |
135 | - ([_player.currentItem status] == AVPlayerItemStatusReadyToPlay)) { | |
136 | - _playButton.enabled = YES; | |
137 | - } | |
138 | - else { | |
139 | - _playButton.enabled = NO; | |
140 | - } | |
100 | + if((_player.currentItem != nil) && | |
101 | + ([_player.currentItem status] == AVPlayerItemStatusReadyToPlay)) { | |
102 | + _playButton.enabled = YES; | |
103 | + } | |
104 | + else { | |
105 | + _playButton.enabled = NO; | |
106 | + } | |
141 | 107 | |
142 | 108 | CGFloat rate = _player.rate; |
143 | 109 | if(_player.rate == 0) { |
@@ -163,6 +129,72 @@ | ||
163 | 129 | |
164 | 130 | #pragma mark - |
165 | 131 | #pragma mark KVC & KVO |
132 | +- (void)setAsset:(AVURLAsset *)asset | |
133 | +{ | |
134 | + if(_asset == asset) return; | |
135 | + | |
136 | + [_asset autorelease]; | |
137 | + _asset = [asset retain]; | |
138 | + | |
139 | + [_asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject: @"tracks"] | |
140 | + completionHandler: | |
141 | + ^ { | |
142 | + dispatch_async(dispatch_get_main_queue(), | |
143 | + ^{ | |
144 | + self.item = [AVPlayerItem playerItemWithAsset:_asset]; | |
145 | + }); | |
146 | + }]; | |
147 | +} | |
148 | +- (void)setItem:(AVPlayerItem *)item | |
149 | +{ | |
150 | + if(_item == item) return; | |
151 | + | |
152 | + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; | |
153 | + if(_item) { | |
154 | + [_item removeObserver:self forKeyPath:@"status"]; | |
155 | + [nc removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:_item]; | |
156 | + } | |
157 | + [_item autorelease]; | |
158 | + _item = [item retain]; | |
159 | + | |
160 | + [_item addObserver:self | |
161 | + forKeyPath:@"status" | |
162 | + options:0 | |
163 | + context:_item]; | |
164 | + [nc addObserver:self | |
165 | + selector:@selector(playerItemDidReachEnd:) | |
166 | + name:AVPlayerItemDidPlayToEndTimeNotification | |
167 | + object:_item]; | |
168 | + | |
169 | + self.player = [AVPlayer playerWithPlayerItem:_item]; | |
170 | +} | |
171 | +- (void)setPlayer:(AVPlayer *)player | |
172 | +{ | |
173 | + if(_player == player) return; | |
174 | + | |
175 | + if(_player) { | |
176 | + [_player removeObserver:self forKeyPath:@"rate"]; | |
177 | + [_player pause]; | |
178 | + } | |
179 | + [_player autorelease]; | |
180 | + _player = [player retain]; | |
181 | + | |
182 | + [_player addObserver:self | |
183 | + forKeyPath:@"rate" | |
184 | + options:0 | |
185 | + context:_player]; | |
186 | + | |
187 | + [_player addPeriodicTimeObserverForInterval:CMTimeMake(4, 100) | |
188 | + queue:dispatch_get_main_queue() | |
189 | + usingBlock: ^(CMTime time) { | |
190 | + Float64 seconds = CMTimeGetSeconds(time); | |
191 | + self.currentTime = seconds; | |
192 | + }]; | |
193 | + | |
194 | + AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player]; | |
195 | + [_view setWantsLayer:YES]; | |
196 | + [_view setLayer:layer]; | |
197 | +} | |
166 | 198 | - (void)setCaption:(NSString *)caption |
167 | 199 | { |
168 | 200 | if(![_view layer]) return; |
@@ -199,18 +231,18 @@ | ||
199 | 231 | { |
200 | 232 | if(context == _item) { |
201 | 233 | dispatch_async(dispatch_get_main_queue(), |
202 | - ^{ | |
234 | + ^{ | |
203 | 235 | [self sizeTofitWidnow]; |
204 | - [self syncUI]; | |
205 | - }); | |
206 | - return; | |
236 | + [self syncUI]; | |
237 | + }); | |
238 | + return; | |
207 | 239 | } |
208 | 240 | if(context == _player) { |
209 | 241 | dispatch_async(dispatch_get_main_queue(), |
210 | - ^{ | |
211 | - [self syncUI]; | |
212 | - }); | |
213 | - return; | |
242 | + ^{ | |
243 | + [self syncUI]; | |
244 | + }); | |
245 | + return; | |
214 | 246 | } |
215 | 247 | [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; |
216 | 248 | } |