Commit ef779091 authored by Felix Paul Kühne's avatar Felix Paul Kühne

macosx: continue playback where you left off, take 2 (close #11478)

parent ff007ad1
......@@ -839,6 +839,9 @@ static VLCMain *_o_sharedMainInstance = nil;
var_DelCallback(p_intf->p_libvlc, "intf-boss", BossCallback, self);
if (p_current_input) {
/* continue playback where you left off */
[[self playlist] storePlaybackPositionForItem:p_current_input];
var_DelCallback(p_current_input, "intf-event", InputEvent, [VLCMain sharedInstance]);
vlc_object_release(p_current_input);
p_current_input = NULL;
......@@ -1281,6 +1284,8 @@ static VLCMain *_o_sharedMainInstance = nil;
}
p_input_changed = vlc_object_hold(p_current_input);
[[self playlist] continuePlaybackWhereYouLeftOff:p_current_input];
}
}
......@@ -1495,45 +1500,11 @@ 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];
[mediaList removeObject:url];
[mediaList addObject:url];
NSUInteger mediaListCount = mediaList.count;
if (mediaListCount > 30) {
for (NSUInteger x = 0; x < mediaListCount - 30; x++) {
[mutDict removeObjectForKey:[mediaList objectAtIndex:0]];
[mediaList removeObjectAtIndex:0];
}
}
} else {
[mutDict removeObjectForKey:url];
[mediaList removeObject:url];
}
[defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
[defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
}
}
if (state == END_S || state == -1) {
/* continue playback where you left off */
if (p_current_input)
[[self playlist] storePlaybackPositionForItem:p_current_input];
if (i_control_itunes > 0) {
if (o_itunes_play_timer) {
[o_itunes_play_timer invalidate];
......
......@@ -139,4 +139,7 @@
- (void)appendNodeArray:(NSArray*)o_array inNode:(playlist_item_t *)p_node atPos:(int)i_position enqueue:(BOOL)b_enqueue;
- (void)setColumn: (NSString *)o_column state: (NSInteger)i_state translationDict:(NSDictionary *)o_dict;
- (void)continuePlaybackWhereYouLeftOff:(input_thread_t *)p_input_thread;
- (void)storePlaybackPositionForItem:(input_thread_t *)p_input_thread;
@end
......@@ -50,6 +50,7 @@
#import "misc.h"
#import "open.h"
#import "MainMenu.h"
#import "CoreInteraction.h"
#include <vlc_keys.h>
#import <vlc_interface.h>
......@@ -853,11 +854,6 @@
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;
......@@ -1098,8 +1094,6 @@
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];
......@@ -1477,33 +1471,46 @@
[o_arrayToSave release];
}
- (void)_continuePlaybackWhereYouLeftOff:(input_item_t *)p_input
- (void)continuePlaybackWhereYouLeftOff:(input_thread_t *)p_input_thread
{
NSDictionary *recentlyPlayedFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentlyPlayedMedia"];
if (recentlyPlayedFiles) {
char *psz_url = decode_URI(input_item_GetURI(p_input));
input_item_t *p_item = input_GetItem(p_input_thread);
if (!p_item)
return;
char *psz_url = decode_URI(input_item_GetURI(p_item));
NSString *url = [NSString stringWithUTF8String:psz_url ? psz_url : ""];
free(psz_url);
NSNumber *lastPosition = [recentlyPlayedFiles objectForKey:url];
if (lastPosition && lastPosition.intValue > 0) {
msg_Dbg(VLCIntf, "last playback position for %s was %i", [url UTF8String], lastPosition.intValue);
vlc_value_t pos;
var_Get(p_input_thread, "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;
if (current_pos_in_sec >= lastPosition.intValue)
return;
int settingValue = config_GetInt(VLCIntf, "macosx-continue-playback");
NSInteger returnValue = NSAlertErrorReturn;
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 \"%@\" will continue at %@"), [NSString stringWithUTF8String:input_item_GetTitleFbName(p_input)], [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
NSAlert *theAlert = [NSAlert alertWithMessageText:_NS("Continue playback?") defaultButton:_NS("Continue") alternateButton:_NS("Restart playback") otherButton:_NS("Always continue") informativeTextWithFormat:_NS("Playback of \"%@\" will continue at %@"), [NSString stringWithUTF8String:input_item_GetTitleFbName(p_item)], [[VLCStringUtility sharedInstance] stringForTime:lastPosition.intValue]];
playlist_t *p_playlist = pl_Get(VLCIntf);
PL_UNLOCK;
[[VLCCoreInteraction sharedInstance] pause];
returnValue = [theAlert runModal];
PL_LOCK;
[[VLCCoreInteraction sharedInstance] playOrPause];
}
if (returnValue == NSAlertAlternateReturn || settingValue == 2)
lastPosition = [NSNumber numberWithInt:0];
input_item_AddOption(p_input, [[NSString stringWithFormat:@"start-time=%i", lastPosition.intValue] UTF8String], VLC_INPUT_OPTION_TRUSTED | VLC_INPUT_OPTION_REPLACE);
pos.f_float = (float)lastPosition.intValue / (float)dur;
msg_Dbg(VLCIntf, "continuing playback at %2.2f", pos.f_float);
var_Set(p_input_thread, "position", pos);
if (returnValue == NSAlertOtherReturn)
config_PutInt(VLCIntf, "macosx-continue-playback", 1);
......@@ -1511,6 +1518,46 @@
}
}
- (void)storePlaybackPositionForItem:(input_thread_t *)p_input_thread
{
input_item_t *p_item = input_GetItem(p_input_thread);
if (!p_item)
return;
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_input_thread, "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];
[mediaList removeObject:url];
[mediaList addObject:url];
NSUInteger mediaListCount = mediaList.count;
if (mediaListCount > 30) {
for (NSUInteger x = 0; x < mediaListCount - 30; x++) {
[mutDict removeObjectForKey:[mediaList objectAtIndex:0]];
[mediaList removeObjectAtIndex:0];
}
}
} else {
[mutDict removeObjectForKey:url];
[mediaList removeObject:url];
}
[defaults setObject:mutDict forKey:@"recentlyPlayedMedia"];
[defaults setObject:mediaList forKey:@"recentlyPlayedMediaList"];
[defaults synchronize];
}
@end
@implementation VLCPlaylist (NSOutlineViewDataSource)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment