VLCMediaPlayer.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLC.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. * Faustion Osuna <enrique.osuna # gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24. *****************************************************************************/
  25. #import "VLCLibrary.h"
  26. #import "VLCMediaPlayer.h"
  27. #import "VLCEventManager.h"
  28. #import "VLCLibVLCBridging.h"
  29. #import "VLCVideoView.h"
  30. #include <vlc/vlc.h>
  31. /* Notification Messages */
  32. NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  33. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  34. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  35. {
  36. static NSString * stateToStrings[] = {
  37. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  38. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  39. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  40. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  41. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  42. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  43. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  44. };
  45. return stateToStrings[state];
  46. }
  47. /* libvlc event callback */
  48. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  49. {
  50. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  51. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  52. withNotificationName:VLCMediaPlayerVolumeChanged];
  53. }
  54. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  55. {
  56. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  57. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  58. withMethod:@selector(mediaPlayerTimeChanged:)
  59. withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_instance_time_changed.new_time]];
  60. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  61. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  62. withNotificationName:VLCMediaPlayerTimeChanged];
  63. [pool release];
  64. }
  65. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  66. {
  67. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  68. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  69. withMethod:@selector(mediaPlayerPositionChanged:)
  70. withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_instance_position_changed.new_position]];
  71. [pool release];
  72. }
  73. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  74. {
  75. VLCMediaPlayerState newState;
  76. if( event->type == libvlc_MediaInstancePlayed )
  77. newState = VLCMediaPlayerStatePlaying;
  78. else if( event->type == libvlc_MediaInstancePaused )
  79. newState = VLCMediaPlayerStatePaused;
  80. else if( event->type == libvlc_MediaInstanceReachedEnd )
  81. newState = VLCMediaPlayerStateStopped;
  82. else
  83. {
  84. NSLog(@"%s: Unknown event", __FUNCTION__);
  85. return;
  86. }
  87. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  88. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  89. withMethod:@selector(mediaPlayerStateChanged:)
  90. withArgumentAsObject:[NSNumber numberWithInt:newState]];
  91. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  92. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  93. withNotificationName:VLCMediaPlayerStateChanged];
  94. [pool release];
  95. }
  96. // TODO: Documentation
  97. @interface VLCMediaPlayer (Private)
  98. - (id)initWithDrawable:(id)aDrawable;
  99. - (void)registerObservers;
  100. - (void)unregisterObservers;
  101. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  102. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  103. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  104. @end
  105. @implementation VLCMediaPlayer
  106. /* Bindings */
  107. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  108. {
  109. static NSDictionary * dict = nil;
  110. NSSet * superKeyPaths;
  111. if( !dict )
  112. {
  113. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  114. [NSSet setWithObject:@"state"], @"playing",
  115. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  116. [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
  117. [NSSet setWithObjects:@"state", @"media", nil], @"description",
  118. nil] retain];
  119. }
  120. if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
  121. {
  122. NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
  123. [ret unionSet:superKeyPaths];
  124. return ret;
  125. }
  126. return [dict objectForKey: key];
  127. }
  128. /* Contructor */
  129. - (id)init
  130. {
  131. return [self initWithDrawable:nil];
  132. }
  133. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  134. {
  135. return [self initWithDrawable: aVideoView];
  136. }
  137. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  138. {
  139. return [self initWithDrawable: aVideoLayer];
  140. }
  141. - (void)release
  142. {
  143. @synchronized(self)
  144. {
  145. if([self retainCount] <= 1)
  146. {
  147. /* We must make sure we won't receive new event after an upcoming dealloc
  148. * We also may receive a -retain in some event callback that may occcur
  149. * Before libvlc_event_detach. So this can't happen in dealloc */
  150. [self unregisterObservers];
  151. }
  152. [super release];
  153. }
  154. }
  155. - (void)dealloc
  156. {
  157. // Always get rid of the delegate first so we can stop sending messages to it
  158. // TODO: Should we tell the delegate that we're shutting down?
  159. delegate = nil;
  160. libvlc_media_instance_release((libvlc_media_instance_t *)instance);
  161. // Get rid of everything else
  162. [media release];
  163. [cachedTime release];
  164. [super dealloc];
  165. }
  166. - (void)setDelegate:(id)value
  167. {
  168. delegate = value;
  169. }
  170. - (id)delegate
  171. {
  172. return delegate;
  173. }
  174. - (void)setVideoView:(VLCVideoView *)aVideoView
  175. {
  176. [self setDrawable: aVideoView];
  177. }
  178. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  179. {
  180. [self setDrawable: aVideoLayer];
  181. }
  182. - (void)setDrawable:(id)aDrawable
  183. {
  184. // Make sure that this instance has been associated with the drawing canvas.
  185. libvlc_exception_t ex;
  186. libvlc_exception_init( &ex );
  187. libvlc_media_instance_set_drawable ((libvlc_media_instance_t *)instance,
  188. (libvlc_drawable_t)aDrawable,
  189. &ex);
  190. catch_exception( &ex );
  191. }
  192. - (id)drawable
  193. {
  194. libvlc_exception_t ex;
  195. libvlc_exception_init( &ex );
  196. libvlc_drawable_t ret = libvlc_media_instance_get_drawable ((libvlc_media_instance_t *)instance,
  197. &ex);
  198. catch_exception( &ex );
  199. return (id)ret;
  200. }
  201. - (void)setVideoAspectRatio:(char *)value
  202. {
  203. libvlc_video_set_aspect_ratio( instance, value, NULL );
  204. }
  205. - (char *)videoAspectRatio
  206. {
  207. libvlc_exception_t ex;
  208. libvlc_exception_init( &ex );
  209. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  210. catch_exception( &ex );
  211. return result;
  212. }
  213. - (void)setVideoSubTitles:(int)value
  214. {
  215. libvlc_video_set_spu( instance, value, NULL );
  216. }
  217. - (int)videoSubTitles
  218. {
  219. libvlc_exception_t ex;
  220. libvlc_exception_init( &ex );
  221. int result = libvlc_video_get_spu( instance, &ex );
  222. catch_exception( &ex );
  223. return result;
  224. }
  225. - (void)setVideoCropGeometry:(char *)value
  226. {
  227. libvlc_video_set_crop_geometry( instance, value, NULL );
  228. }
  229. - (char *)videoCropGeometry
  230. {
  231. libvlc_exception_t ex;
  232. libvlc_exception_init( &ex );
  233. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  234. catch_exception( &ex );
  235. return result;
  236. }
  237. - (void)setVideoTeleText:(int)value
  238. {
  239. libvlc_video_set_teletext( instance, value, NULL );
  240. }
  241. - (int)videoTeleText
  242. {
  243. libvlc_exception_t ex;
  244. libvlc_exception_init( &ex );
  245. int result = libvlc_video_get_teletext( instance, &ex );
  246. catch_exception( &ex );
  247. return result;
  248. }
  249. - (void)setRate:(float)value
  250. {
  251. libvlc_media_instance_set_rate( instance, value, NULL );
  252. }
  253. - (float)rate
  254. {
  255. libvlc_exception_t ex;
  256. libvlc_exception_init( &ex );
  257. float result = libvlc_media_instance_get_rate( instance, &ex );
  258. catch_exception( &ex );
  259. return result;
  260. }
  261. - (NSSize)videoSize
  262. {
  263. libvlc_exception_t ex;
  264. libvlc_exception_init( &ex );
  265. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_instance_t *)instance, &ex),
  266. libvlc_video_get_width((libvlc_media_instance_t *)instance, &ex));
  267. catch_exception( &ex );
  268. return result;
  269. }
  270. - (BOOL)hasVideoOut
  271. {
  272. libvlc_exception_t ex;
  273. libvlc_exception_init( &ex );
  274. BOOL result = libvlc_media_instance_has_vout((libvlc_media_instance_t *)instance, &ex);
  275. if (libvlc_exception_raised( &ex ))
  276. {
  277. libvlc_exception_clear( &ex );
  278. return NO;
  279. }
  280. else
  281. return result;
  282. }
  283. - (float)framesPerSecond
  284. {
  285. libvlc_exception_t ex;
  286. libvlc_exception_init( &ex );
  287. float result = libvlc_media_instance_get_fps( (libvlc_media_instance_t *)instance, &ex );
  288. catch_exception( &ex );
  289. return result;
  290. }
  291. - (void)setTime:(VLCTime *)value
  292. {
  293. libvlc_exception_t ex;
  294. libvlc_exception_init( &ex );
  295. // Time is managed in seconds, while duration is managed in microseconds
  296. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  297. libvlc_media_instance_set_time( (libvlc_media_instance_t *)instance,
  298. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  299. &ex );
  300. catch_exception( &ex );
  301. }
  302. - (VLCTime *)time
  303. {
  304. return cachedTime;
  305. }
  306. - (void)setChapter:(int)value;
  307. {
  308. libvlc_media_instance_set_chapter( instance, value, NULL );
  309. }
  310. - (int)chapter
  311. {
  312. libvlc_exception_t ex;
  313. libvlc_exception_init( &ex );
  314. int result = libvlc_media_instance_get_chapter( instance, &ex );
  315. catch_exception( &ex );
  316. return result;
  317. }
  318. - (int)countOfChapters
  319. {
  320. libvlc_exception_t ex;
  321. libvlc_exception_init( &ex );
  322. int result = libvlc_media_instance_get_chapter_count( instance, &ex );
  323. catch_exception( &ex );
  324. return result;
  325. }
  326. - (void)setAudioTrack:(int)value
  327. {
  328. libvlc_audio_set_track( instance, value, NULL );
  329. }
  330. - (int)audioTrack
  331. {
  332. libvlc_exception_t ex;
  333. libvlc_exception_init( &ex );
  334. int result = libvlc_audio_get_track( instance, &ex );
  335. catch_exception( &ex );
  336. return result;
  337. }
  338. - (int)countOfAudioTracks
  339. {
  340. libvlc_exception_t ex;
  341. libvlc_exception_init( &ex );
  342. int result = libvlc_audio_get_track_count( instance, &ex );
  343. catch_exception( &ex );
  344. return result;
  345. }
  346. - (void)setAudioChannel:(int)value
  347. {
  348. libvlc_audio_set_channel( instance, value, NULL );
  349. }
  350. - (int)audioChannel
  351. {
  352. libvlc_exception_t ex;
  353. libvlc_exception_init( &ex );
  354. int result = libvlc_audio_get_channel( instance, &ex );
  355. catch_exception( &ex );
  356. return result;
  357. }
  358. - (void)setMedia:(VLCMedia *)value
  359. {
  360. // We only know how to play media files...not media resources with subitems
  361. if (media != value && [media subitems] == nil)
  362. {
  363. if (media && [media compare:value] == NSOrderedSame)
  364. return;
  365. [media release];
  366. media = [value retain];
  367. libvlc_exception_t ex;
  368. libvlc_exception_init( &ex );
  369. libvlc_media_instance_set_media_descriptor( instance, [media libVLCMediaDescriptor], &ex );
  370. catch_exception( &ex );
  371. }
  372. }
  373. - (VLCMedia *)media
  374. {
  375. return media;
  376. }
  377. - (BOOL)play
  378. {
  379. libvlc_exception_t ex;
  380. libvlc_exception_init( &ex );
  381. libvlc_media_instance_play( (libvlc_media_instance_t *)instance, &ex );
  382. catch_exception( &ex );
  383. return YES;
  384. }
  385. - (void)pause
  386. {
  387. if( [NSThread isMainThread] )
  388. {
  389. /* Hack because we create a dead lock here, when the vout is stopped
  390. * and tries to recontact us on the main thread */
  391. /* FIXME: to do this properly we need to do some locking. We may want
  392. * to move that to libvlc */
  393. [self performSelectorInBackground:@selector(pause) withObject:nil];
  394. return;
  395. }
  396. // Return if there is no media available or if the stream is not paused or
  397. // playing something else
  398. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  399. return;
  400. // Should never get here.
  401. if (!instance)
  402. return;
  403. // Pause the stream
  404. libvlc_exception_t ex;
  405. libvlc_exception_init( &ex );
  406. libvlc_media_instance_pause( (libvlc_media_instance_t *)instance, &ex );
  407. catch_exception( &ex );
  408. // TODO: Should we record the time in case the media instance is destroyed
  409. // then rebuilt?
  410. }
  411. - (void)stop
  412. {
  413. if( [NSThread isMainThread] )
  414. {
  415. /* Hack because we create a dead lock here, when the vout is stopped
  416. * and tries to recontact us on the main thread */
  417. /* FIXME: to do this properly we need to do some locking. We may want
  418. * to move that to libvlc */
  419. [self performSelectorInBackground:@selector(stop) withObject:nil];
  420. return;
  421. }
  422. // Return if there is no media available or if the system is not in play status
  423. // or pause status.
  424. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  425. return;
  426. // The following is not implemented in the core, should I fix it or just
  427. // compensate?
  428. // libvlc_exception_t ex;
  429. // libvlc_exception_init( &ex );
  430. // libvlc_media_instance_stop((libvlc_media_instance_t *)instance, &ex);
  431. // catch_exception( &ex );
  432. // Pause and reposition to the begining of the stream.
  433. [self pause];
  434. [self setTime:0];
  435. // TODO: Should we pause this or destroy the media instance so that it appears as being "stopped"?
  436. }
  437. - (void)fastForward
  438. {
  439. [self fastForwardAtRate: 2.0];
  440. }
  441. - (void)fastForwardAtRate:(float)rate
  442. {
  443. [self setRate:rate];
  444. }
  445. - (void)rewind
  446. {
  447. [self rewindAtRate: 2.0];
  448. }
  449. - (void)rewindAtRate:(float)rate
  450. {
  451. [self setRate: -rate];
  452. }
  453. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  454. {
  455. return [NSSet setWithObjects:@"state", nil];
  456. }
  457. - (BOOL)isPlaying
  458. {
  459. VLCMediaPlayerState state = [self state];
  460. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  461. (state == VLCMediaPlayerStatePlaying));
  462. }
  463. - (BOOL)willPlay
  464. {
  465. libvlc_exception_t ex;
  466. libvlc_exception_init( &ex );
  467. BOOL ret = libvlc_media_instance_will_play( (libvlc_media_instance_t *)instance, &ex );
  468. if (libvlc_exception_raised(&ex))
  469. {
  470. libvlc_exception_clear(&ex);
  471. return NO;
  472. }
  473. else
  474. return ret;
  475. }
  476. static const VLCMediaPlayerState libvlc_to_local_state[] =
  477. {
  478. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  479. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  480. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  481. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  482. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  483. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  484. [libvlc_Error] = VLCMediaPlayerStateError
  485. };
  486. - (VLCMediaPlayerState)state
  487. {
  488. return cachedState;
  489. }
  490. - (float)position
  491. {
  492. return position;
  493. }
  494. - (void)setPosition:(float)newPosition
  495. {
  496. libvlc_exception_t ex;
  497. libvlc_exception_init( &ex );
  498. libvlc_media_instance_set_position( instance, newPosition, &ex );
  499. catch_exception( &ex );
  500. }
  501. - (BOOL)isSeekable
  502. {
  503. libvlc_exception_t ex;
  504. libvlc_exception_init( &ex );
  505. BOOL ret = libvlc_media_instance_is_seekable( instance, &ex );
  506. catch_exception( &ex );
  507. return ret;
  508. }
  509. - (BOOL)canPause
  510. {
  511. libvlc_exception_t ex;
  512. libvlc_exception_init( &ex );
  513. BOOL ret = libvlc_media_instance_can_pause( instance, &ex );
  514. catch_exception( &ex );
  515. return ret;
  516. }
  517. @end
  518. @implementation VLCMediaPlayer (Private)
  519. - (id)initWithDrawable:(id)aDrawable
  520. {
  521. if (self = [super init])
  522. {
  523. delegate = nil;
  524. media = nil;
  525. cachedTime = [[VLCTime nullTime] retain];
  526. position = 0.0f;
  527. cachedState = VLCMediaPlayerStateStopped;
  528. // Create a media instance, it doesn't matter what library we start off with
  529. // it will change depending on the media descriptor provided to the media
  530. // instance
  531. libvlc_exception_t ex;
  532. libvlc_exception_init( &ex );
  533. instance = (void *)libvlc_media_instance_new([VLCLibrary sharedInstance], &ex);
  534. catch_exception( &ex );
  535. [self registerObservers];
  536. [self setDrawable:aDrawable];
  537. }
  538. return self;
  539. }
  540. - (void)registerObservers
  541. {
  542. libvlc_exception_t ex;
  543. libvlc_exception_init( &ex );
  544. // Attach event observers into the media instance
  545. libvlc_event_manager_t * p_em = libvlc_media_instance_event_manager( instance, &ex );
  546. libvlc_event_attach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex );
  547. libvlc_event_attach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex );
  548. libvlc_event_attach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex );
  549. /* FIXME: We may want to turn that off when none is interested by that */
  550. libvlc_event_attach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, &ex );
  551. libvlc_event_attach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, &ex );
  552. catch_exception( &ex );
  553. }
  554. - (void)unregisterObservers
  555. {
  556. libvlc_event_manager_t * p_em = libvlc_media_instance_event_manager( instance, NULL );
  557. libvlc_event_detach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL );
  558. libvlc_event_detach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL );
  559. libvlc_event_detach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL );
  560. libvlc_event_detach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, NULL );
  561. libvlc_event_detach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, NULL );
  562. }
  563. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  564. {
  565. [self willChangeValueForKey:@"time"];
  566. [cachedTime release];
  567. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  568. [self didChangeValueForKey:@"time"];
  569. }
  570. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  571. {
  572. if( [newPosition floatValue] - position < 0.005 && position - [newPosition floatValue] < 0.005 )
  573. return; /* Forget that, this is too much precision for our uses */
  574. [self willChangeValueForKey:@"position"];
  575. position = ((float)((int)([newPosition floatValue]*1000)))/1000.;
  576. [self didChangeValueForKey:@"position"];
  577. }
  578. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  579. {
  580. [self willChangeValueForKey:@"state"];
  581. cachedState = [newState intValue];
  582. [self didChangeValueForKey:@"state"];
  583. }
  584. @end