• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

CocoaでAVFoundationを使うサンプル


Commit MetaInfo

修訂152e2d6f124296060c9e0d5fb122dbec3bd77c63 (tree)
時間2012-06-11 21:41:30
作者Hori <masaki@MBP....>
CommiterHori

Log Message

ソースコード整理

Change Summary

差異

--- a/testAVFoundation/HMDocument.m
+++ b/testAVFoundation/HMDocument.m
@@ -26,8 +26,6 @@
2626 @implementation HMDocument
2727 @synthesize view = _view;
2828 @synthesize playButton = _playButton;
29-@synthesize timeGage = _timeGage;
30-@synthesize timeBar = _timeBar;
3129 @synthesize currentTime = _currentTime;
3230
3331 @synthesize asset = _asset;
@@ -38,6 +36,10 @@
3836
3937 - (void)dealloc
4038 {
39+ [[NSNotificationCenter defaultCenter] removeObserver:self
40+ name:AVPlayerItemDidPlayToEndTimeNotification
41+ object:_item];
42+
4143 [_player release];
4244 [_item release];
4345 [_asset release];
@@ -66,42 +68,6 @@
6668 {
6769 self.asset = [AVURLAsset URLAssetWithURL:url options:nil];
6870
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-
10571 return _asset ? YES : NO;
10672 }
10773
@@ -131,13 +97,13 @@
13197 }
13298 - (void)syncUI
13399 {
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+ }
141107
142108 CGFloat rate = _player.rate;
143109 if(_player.rate == 0) {
@@ -163,6 +129,72 @@
163129
164130 #pragma mark -
165131 #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+}
166198 - (void)setCaption:(NSString *)caption
167199 {
168200 if(![_view layer]) return;
@@ -199,18 +231,18 @@
199231 {
200232 if(context == _item) {
201233 dispatch_async(dispatch_get_main_queue(),
202- ^{
234+ ^{
203235 [self sizeTofitWidnow];
204- [self syncUI];
205- });
206- return;
236+ [self syncUI];
237+ });
238+ return;
207239 }
208240 if(context == _player) {
209241 dispatch_async(dispatch_get_main_queue(),
210- ^{
211- [self syncUI];
212- });
213- return;
242+ ^{
243+ [self syncUI];
244+ });
245+ return;
214246 }
215247 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
216248 }