Skip to content

Commit

Permalink
Play next/prev media via key binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbirec committed May 7, 2016
1 parent 992ef92 commit 7be6d74
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
7 changes: 7 additions & 0 deletions PeerPlayer/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

30 changes: 25 additions & 5 deletions PeerPlayer/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
}
}

Expand Down
2 changes: 2 additions & 0 deletions PeerPlayer/Playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@property (strong) NSArray<File*>* mediaFiles;
@property (strong) NSArray<File*>* subFiles;

-(File*) getFirstMedia;
-(File*) getPrevMedia:(File*) currentFile;
-(File*) getNextMedia:(File*) currentFile;
-(File*) getSubtitleForMedia:(File*) mediaFile;

Expand Down
32 changes: 30 additions & 2 deletions PeerPlayer/Playlist.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,45 @@ +(Playlist*) playListFromFiles:(NSArray<File*>*) 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) {
return [self.mediaFiles firstObject];
}

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 {
Expand Down
8 changes: 8 additions & 0 deletions PeerPlayer/RootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "RootView.h"
#import "MpvController.h"
#import "AppDelegate.h"

@implementation RootView

Expand All @@ -28,6 +29,7 @@ -(void) awakeFromNib {
-(void) keyDown:(NSEvent *)event
{
NSLog(@"keydown: %@", event);
AppDelegate* delegate = [[NSApplication sharedApplication] delegate];

switch(event.keyCode) {
// Space
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand Down

0 comments on commit 7be6d74

Please sign in to comment.