Przeglądaj źródła

Sources/VLCMedia.m: Implement a KeyValueCoding enabled -state property.

Pierre d'Herbemont 17 lat temu
rodzic
commit
6de31c5977
2 zmienionych plików z 57 dodań i 2 usunięć
  1. 16 1
      Headers/Public/VLCMedia.h
  2. 41 1
      Sources/VLCMedia.m

+ 16 - 1
Headers/Public/VLCMedia.h

@@ -61,6 +61,14 @@ extern NSString *VLCMediaMetaChanged;        //< Notification message for when t
 @class VLCMediaList;
 @class VLCMedia;
 
+typedef enum VLCMediaState
+{
+    VLCMediaStateNothingSpecial,       //< Nothing
+    VLCMediaStateBuffering,            //< Stream is buffering
+    VLCMediaStatePlaying,              //< Stream is playing
+    VLCMediaStateError,                //< Can't be played because an error occured
+} VLCMediaState;
+
 /**
  * Informal protocol declaration for VLCMedia delegates.  Allows data changes to be
  * trapped.
@@ -108,6 +116,7 @@ extern NSString *VLCMediaMetaChanged;        //< Notification message for when t
     NSMutableDictionary * metaDictionary;    //< Meta data storage
     id                    delegate;          //< Delegate object
     BOOL                  preparsed;         //< Value used to determine of the file has been preparsed
+    VLCMediaState         state;
 }
 
 /* Object Factories */
@@ -195,4 +204,10 @@ extern NSString *VLCMediaMetaChanged;        //< Notification message for when t
  * \return The receiver's meta data as a NSDictionary object.
  */
 - (NSDictionary *)metaDictionary;
-@end
+
+/**
+ * Returns the receiver's state.
+ * \return The receiver's state, such as Playing, Error, NothingSpecial, Buffering.
+ */
+- (VLCMediaState)state;
+@end

+ 41 - 1
Sources/VLCMedia.m

@@ -72,6 +72,22 @@ NSString *VLCMediaMetaChanged           = @"VLCMediaMetaChanged";
 - (void)metaChanged:(NSString *)metaType;
 @end
 
+static VLCMediaState libvlc_state_to_media_state[] =
+{
+    [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
+    [libvlc_Stopped]        = VLCMediaStateNothingSpecial,
+    [libvlc_Opening]        = VLCMediaStateNothingSpecial,
+    [libvlc_Buffering]      = VLCMediaStateBuffering,
+    [libvlc_Ended]          = VLCMediaStateNothingSpecial,
+    [libvlc_Error]          = VLCMediaStateError,
+    [libvlc_Playing]        = VLCMediaStatePlaying,
+    [libvlc_Paused]         = VLCMediaStatePlaying,
+};
+
+static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
+{
+    return libvlc_state_to_media_state[state];
+}
 
 /******************************************************************************
  * LibVLC Event Callback
@@ -96,6 +112,16 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
     [pool release];
 }
 
+static void HandleMediaStateChanged(const libvlc_event_t *event, void *self)
+{
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    [[VLCEventManager sharedManager] callOnMainThreadObject:self
+                                                 withMethod:@selector(setState:)
+                                       withArgumentAsObject:[NSNumber numberWithInt:
+                                            LibVLCStateToMediaState(event->u.media_descriptor_state_changed.new_state)]];
+    [pool release];
+}
+
 /******************************************************************************
  * Implementation
  */
@@ -179,6 +205,7 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
             libvlc_event_manager_t *p_em = libvlc_media_descriptor_event_manager(p_md, NULL);
             libvlc_event_detach(p_em, libvlc_MediaDescriptorMetaChanged,     HandleMediaMetaChanged,     self, NULL);
             libvlc_event_detach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, NULL);
+            libvlc_event_detach(p_em, libvlc_MediaDescriptorStateChanged,    HandleMediaStateChanged,    self, NULL);
         }
         [super release];
     }
@@ -283,6 +310,11 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
 {
     return delegate;
 }
+
+- (VLCMediaState)state
+{
+    return state;
+}
 @end
 
 /******************************************************************************
@@ -394,6 +426,7 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
     libvlc_event_manager_t *p_em = libvlc_media_descriptor_event_manager( p_md, &ex );
     libvlc_event_attach(p_em, libvlc_MediaDescriptorMetaChanged,     HandleMediaMetaChanged,     self, &ex);
     libvlc_event_attach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, &ex);
+    libvlc_event_attach(p_em, libvlc_MediaDescriptorStateChanged,    HandleMediaStateChanged,    self, &ex);
     quit_on_exception( &ex );
     
     libvlc_media_list_t *p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
@@ -405,7 +438,7 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
         subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
         libvlc_media_list_release( p_mlist );
     }
-    
+    state = LibVLCStateToMediaState(libvlc_media_descriptor_get_state( p_md, NULL ));
     /* Force VLCMetaInformationTitle, that will trigger preparsing
      * And all the other meta will be added through the libvlc event system */
     [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
@@ -474,4 +507,11 @@ static void HandleMediaDurationChanged(const libvlc_event_t *event, void *self)
     length = value ? [value retain] : nil;
     [self didChangeValueForKey:@"length"];
 }
+
+- (void)setState:(NSNumber *)newStateAsNumber
+{        
+    [self willChangeValueForKey:@"state"];
+    state = [newStateAsNumber intValue];
+    [self didChangeValueForKey:@"state"];
+}
 @end