From 7be6d749ac1e3658f287a982400f1f770b9ea537 Mon Sep 17 00:00:00 2001 From: Heehong Moon Date: Sun, 8 May 2016 05:06:26 +0900 Subject: [PATCH] Play next/prev media via key binding. --- PeerPlayer/AppDelegate.h | 7 +++++++ PeerPlayer/AppDelegate.m | 30 +++++++++++++++++++++++++----- PeerPlayer/Playlist.h | 2 ++ PeerPlayer/Playlist.m | 32 ++++++++++++++++++++++++++++++-- PeerPlayer/RootView.m | 8 ++++++++ 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/PeerPlayer/AppDelegate.h b/PeerPlayer/AppDelegate.h index 05d3900..9b102ce 100644 --- a/PeerPlayer/AppDelegate.h +++ b/PeerPlayer/AppDelegate.h @@ -38,5 +38,12 @@ -(void) playTorrent:(NSString*)url; -(IBAction) openTorrentFile:(id)sender; -(IBAction) stopCurrentVideo:(id)sender; + + +-(BOOL) hasPrev; +-(BOOL) hasNext; +-(void) playPrev; +-(void) playNext; + @end diff --git a/PeerPlayer/AppDelegate.m b/PeerPlayer/AppDelegate.m index 1a5beec..faa2f85 100644 --- a/PeerPlayer/AppDelegate.m +++ b/PeerPlayer/AppDelegate.m @@ -65,6 +65,29 @@ -(void) loadSubtitle:(File*) file { [dataTask resume]; } +-(BOOL) hasPrev { + return [self.playlist getPrevMedia:self.selectedMedia] != nil; +} + +-(BOOL) hasNext { + return [self.playlist getNextMedia:self.selectedMedia] != nil; +} + + +-(void) playPrev { + File* next = [self.playlist getPrevMedia:self.selectedMedia]; + if(next != nil) { + [self playFile:next]; + } +} + +-(void) playNext { + File* next = [self.playlist getNextMedia:self.selectedMedia]; + if(next != nil) { + [self playFile:next]; + } +} + #pragma Menu -(void) updateMenu { @@ -141,7 +164,7 @@ -(void) torrentReady:(NSDictionary*)data { [self updateMenu]; // Start the first media in the playlist. - File* f = [self.playlist getNextMedia:nil]; + File* f = [self.playlist getFirstMedia]; if(f != nil) { [self playFile:f]; } @@ -180,10 +203,7 @@ -(void) playEnded:(PlayEndReason)reason { NSLog(@"Play ended : %ld", reason); if(reason == kPlayEndEOF) { // Play the next media automatically. - File* next = [self.playlist getNextMedia:self.selectedMedia]; - if(next != nil) { - [self playFile:next]; - } + [self playNext]; } } diff --git a/PeerPlayer/Playlist.h b/PeerPlayer/Playlist.h index 849e825..f8903c8 100644 --- a/PeerPlayer/Playlist.h +++ b/PeerPlayer/Playlist.h @@ -25,6 +25,8 @@ @property (strong) NSArray* mediaFiles; @property (strong) NSArray* subFiles; +-(File*) getFirstMedia; +-(File*) getPrevMedia:(File*) currentFile; -(File*) getNextMedia:(File*) currentFile; -(File*) getSubtitleForMedia:(File*) mediaFile; diff --git a/PeerPlayer/Playlist.m b/PeerPlayer/Playlist.m index a1680ef..dfb2d04 100644 --- a/PeerPlayer/Playlist.m +++ b/PeerPlayer/Playlist.m @@ -61,6 +61,28 @@ +(Playlist*) playListFromFiles:(NSArray*) files { return instance; } +-(File*) getFirstMedia { + return [self.mediaFiles firstObject]; +} + +-(File*) getPrevMedia:(File*) currentFile { + if(currentFile == nil) { + return [self.mediaFiles firstObject]; + } + else { + NSUInteger idx = [self.mediaFiles indexOfObject:currentFile]; + if(idx == NSNotFound) { + return nil; + } + else if(idx == 0) { + return nil; + } + else { + return [self.mediaFiles objectAtIndex:idx-1]; + } + } +} + -(File*) getNextMedia:(File*) currentFile { // Return the first file if(currentFile == nil) { @@ -68,10 +90,16 @@ -(File*) getNextMedia:(File*) currentFile { } NSUInteger idx = [self.mediaFiles indexOfObject:currentFile]; - if(idx + 1 < [self.mediaFiles count]) { + if(idx == NSNotFound) { + return nil; + } + else if(idx + 1 == [self.mediaFiles count]) { + return nil; + + } + else { return [self.mediaFiles objectAtIndex:idx+1]; } - return nil; } -(File*) getSubtitleForMedia:(File*) mediaFile { diff --git a/PeerPlayer/RootView.m b/PeerPlayer/RootView.m index 4ef6970..a23ba46 100644 --- a/PeerPlayer/RootView.m +++ b/PeerPlayer/RootView.m @@ -8,6 +8,7 @@ #import "RootView.h" #import "MpvController.h" +#import "AppDelegate.h" @implementation RootView @@ -28,6 +29,7 @@ -(void) awakeFromNib { -(void) keyDown:(NSEvent *)event { NSLog(@"keydown: %@", event); + AppDelegate* delegate = [[NSApplication sharedApplication] delegate]; switch(event.keyCode) { // Space @@ -39,6 +41,9 @@ -(void) keyDown:(NSEvent *)event if([event modifierFlags] & NSShiftKeyMask) { [[MpvController getInstance] seek:-10]; } + else if([event modifierFlags] & NSCommandKeyMask) { + [delegate playPrev]; + } else { [[MpvController getInstance] seek:-1]; } @@ -48,6 +53,9 @@ -(void) keyDown:(NSEvent *)event if([event modifierFlags] & NSShiftKeyMask) { [[MpvController getInstance] seek:10]; } + else if([event modifierFlags] & NSCommandKeyMask) { + [delegate playNext]; + } else { [[MpvController getInstance] seek:1]; }