[vlc-commits] macosx: add continue playback where you left off (refs #5315)

Felix Paul Kühne git at videolan.org
Sat Apr 26 19:01:57 CEST 2014


vlc | branch: master | Felix Paul Kühne <fkuehne at videolan.org> | Sat Apr 26 18:54:32 2014 +0200| [fa4d67b2efb16b7378f0fa289c9815d6b183bc53] | committer: Felix Paul Kühne

macosx: add continue playback where you left off (refs #5315)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=fa4d67b2efb16b7378f0fa289c9815d6b183bc53
---

 modules/gui/macosx/intf.m     |   40 ++++++++++++++++++++++++++++++++++++++
 modules/gui/macosx/macosx.m   |   11 +++++++++++
 modules/gui/macosx/playlist.m |   43 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/modules/gui/macosx/intf.m b/modules/gui/macosx/intf.m
index 51b4433..dbab132 100644
--- a/modules/gui/macosx/intf.m
+++ b/modules/gui/macosx/intf.m
@@ -1497,6 +1497,46 @@ static VLCMain *_o_sharedMainInstance = nil;
             IOPMAssertionRelease(systemSleepAssertionID);
         }
 
+        /* continue playback where you left off */
+        if (p_current_input) {
+            input_item_t *p_item = input_GetItem(p_current_input);
+            if (p_item) {
+                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+                NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:@"recentlyPlayedMedia"]];
+
+                char *psz_url = decode_URI(input_item_GetURI(p_item));
+                NSString *url = [NSString stringWithUTF8String:psz_url ? psz_url : ""];
+                free(psz_url);
+                vlc_value_t pos;
+                var_Get(p_current_input, "position", &pos);
+                float f_current_pos = 100. * pos.f_float;
+                long long int dur = input_item_GetDuration(p_item) / 1000000;
+                int current_pos_in_sec = (f_current_pos * dur) / 100;
+                NSMutableArray *mediaList = [defaults objectForKey:@"recentlyPlayedMediaList"];
+
+                if (pos.f_float > .05 && pos.f_float < .95 && dur > 180) {
+                    [mutDict setObject:[NSNumber numberWithInt:current_pos_in_sec] forKey:url];
+                    if ([mediaList containsObject:url])
+                         [mediaList removeObject:url];
+                    [mediaList addObject:url];
+                    if (mediaList.count > 30) {
+                        NSUInteger count = mediaList.count;
+                        for (NSUInteger x = 0; x < 30 - count; x++) {
+                            [mutDict removeObjectForKey:mediaList[x]];
+                            [mediaList removeObject:mediaList[x]];
+                        }
+                    }
+                } else {
+                    if ([mediaList containsObject:url]) {
+                        [mutDict removeObjectForKey:url];
+                        [mediaList removeObject:url];
+                    }
+                }
+                [defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
+                [defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
+            }
+        }
+
         if (state == END_S || state == -1) {
             if (i_control_itunes > 0) {
                 if (o_itunes_play_timer) {
diff --git a/modules/gui/macosx/macosx.m b/modules/gui/macosx/macosx.m
index 70ac03c..732e3e4 100644
--- a/modules/gui/macosx/macosx.m
+++ b/modules/gui/macosx/macosx.m
@@ -129,6 +129,15 @@ static const char *const itunes_list_text[] = {
     N_("Do nothing"), N_("Pause iTunes / Spotify"), N_("Pause and resume iTunes / Spotify")
 };
 
+#define CONTINUE_PLAYBACK_TEXT N_("Continue playback where you left off")
+#define CONTINUE_PLAYBACK_LONGTEXT N_("VLC will store playback positions of the last 30 items you played. If you re-open one of those, playback will continue.")
+
+static const int continue_playback_list[] =
+{ 0, 1, 2 };
+static const char *const continue_playback_list_text[] = {
+    N_("Ask"), N_("Always"), N_("Never")
+};
+
 #define VOLUME_MAX_TEXT N_("Maximum Volume displayed")
 
 
@@ -161,6 +170,8 @@ vlc_module_begin()
         add_bool("macosx-lock-aspect-ratio", true, LOCK_ASPECT_RATIO_TEXT, LOCK_ASPECT_RATIO_TEXT, true)
         add_integer("macosx-control-itunes", 1, ITUNES_TEXT, ITUNES_LONGTEXT, false)
         change_integer_list(itunes_list, itunes_list_text)
+        add_integer("macosx-continue-playback", 0, CONTINUE_PLAYBACK_TEXT, CONTINUE_PLAYBACK_LONGTEXT, false)
+        change_integer_list(continue_playback_list, continue_playback_list_text)
 
     set_section(N_("Apple Remote and media keys"), 0)
         add_bool("macosx-appleremote", true, USE_APPLE_REMOTE_TEXT, USE_APPLE_REMOTE_LONGTEXT, false)
diff --git a/modules/gui/macosx/playlist.m b/modules/gui/macosx/playlist.m
index 4086809..0b84608 100644
--- a/modules/gui/macosx/playlist.m
+++ b/modules/gui/macosx/playlist.m
@@ -825,6 +825,12 @@
             else
                 p_item = NULL;
         }
+
+        /* continue playback where you left off */
+        input_item_t *p_input = p_item->p_input;
+        if (p_input)
+            [self _continuePlaybackWhereYouLeftOff:p_input];
+
         playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, p_node, p_item);
     }
     PL_UNLOCK;
@@ -1065,6 +1071,8 @@
             input_item_AddOption(p_input, [[o_options objectAtIndex:i] UTF8String], VLC_INPUT_OPTION_TRUSTED);
     }
 
+    [self _continuePlaybackWhereYouLeftOff:p_input];
+
     /* Recent documents menu */
     if (o_nsurl != nil && (BOOL)config_GetInt(p_playlist, "macosx-recentitems") == YES)
         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_nsurl];
@@ -1121,6 +1129,7 @@
         NSDictionary *o_one_item;
 
         /* Get the item */
+        PL_LOCK;
         o_one_item = [o_array objectAtIndex:i_item];
         p_input = [self createItem: o_one_item];
 
@@ -1128,7 +1137,6 @@
             continue;
 
         /* Add the item */
-        PL_LOCK;
         playlist_NodeAddInput(p_playlist, p_input, p_node,
                                       PLAYLIST_INSERT,
                                       i_position == -1 ?
@@ -1436,6 +1444,39 @@
     [o_arrayToSave release];
 }
 
+- (void)_continuePlaybackWhereYouLeftOff:(input_item_t *)p_input
+{
+    NSDictionary *recentlyPlayedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentlyPlayedMedia"];
+    if (recentlyPlayedFiles) {
+        char *psz_url = decode_URI(input_item_GetURI(p_input));
+        NSString *url = [NSString stringWithUTF8String:psz_url ? psz_url : ""];
+        free(psz_url);
+
+        NSNumber *lastPosition = [recentlyPlayedFiles objectForKey:url];
+        if (lastPosition.intValue > 0) {
+            msg_Dbg(VLCIntf, "last playback position for %s was %i", [url UTF8String], lastPosition.intValue);
+
+            int settingValue = config_GetInt(VLCIntf, "macosx-continue-playback");
+            NSInteger returnValue = 0;
+
+            if (settingValue == 0) {
+                NSAlert *theAlert = [NSAlert alertWithMessageText:_NS("Continue playback?") defaultButton:_NS("Continue") alternateButton:_NS("Restart playback") otherButton:_NS("Always continue") informativeTextWithFormat:_NS("Playback of \"%s\" will continue at %@"), input_item_GetTitleFbName(p_input), [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
+
+                playlist_t *p_playlist = pl_Get(VLCIntf);
+                PL_UNLOCK;
+                returnValue = [theAlert runModal];
+                PL_LOCK;
+            }
+
+            if (returnValue !=0 || settingValue == 1)
+                input_item_AddOption(p_input, [[NSString stringWithFormat:@"start-time=%i", lastPosition.intValue] UTF8String], VLC_INPUT_OPTION_TRUSTED);
+
+            if (returnValue == -1)
+                config_PutInt(VLCIntf, "macosx-continue-playback", 1);
+        }
+    }
+}
+
 @end
 
 @implementation VLCPlaylist (NSOutlineViewDataSource)



More information about the vlc-commits mailing list