VLCMediaPlayer.m 20 KB

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