<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">Mac下的mp3播放器,同步显示lrc歌词。因需要将Finder中的歌曲拖放入tableView,因此实现此功能。</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">Mac下的mp3播放器,同步显示lrc歌词。因需要将Finder中的歌曲拖放入tableView,因此实现此功能。</span>
主要需要一个类:
/* DragDropTableView */
/* DragDropTableView */
#import <Cocoa/Cocoa.h>
@interface DragDropTableView : NSTableView
{
}
@end
#import "DragDropTableView.h"
@implementation DragDropTableView
- (void) awakeFromNib
{
// Register to accept filename drag/drop
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
[self setTarget:self];
[self setDoubleAction:@selector(double_clicked)];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
// Need the delegate hooked up to accept the dragged item(s) into the model
if ([self delegate]==nil)
{
return NSDragOperationNone;
}
if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType])
{
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
// Work around a bug from 10.2 onwards
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
return NSDragOperationEvery;
}
// Stop the NSTableView implementation getting in the way
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
return [self draggingEntered:sender];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
int i;
NSPasteboard *pboard;
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType])
{
id delegate = [self delegate];
NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
if ([delegate respondsToSelector:@selector(acceptFilenameDrag:)])
{
for (i=0;i<[filenames count];i++)
{
[delegate performSelector:@selector(acceptFilenameDrag:) withObject:[filenames objectAtIndex:i]];
}
}
return YES;
}
return NO;
}
- (void)double_clicked
{
NSInteger rowNumber = [self clickedRow];
NSLog(@"double clicked %ld\n", rowNumber);
if (rowNumber > -1) {
[self.delegate performSelector:@selector(tableView_doubleClicked:) withObject:(id)rowNumber];
}
}
- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect
{
NSColor *myColor = [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1.0f];
NSColor *color = (row % 2) ? myColor : [NSColor blackColor];
[color setFill];
NSRectFill([self rectOfRow:row]);
[super drawRow:row clipRect:clipRect];
}
@end
- (void) acceptFilenameDrag:(NSString *) filename; // also a delegate function,
{
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[self getFileName:filename], @"name",
[mp3_info getTimeLong:filename], @"time",
filename, @"pathname",
nil];
[arrayController_filenames addObject:dict];
[fileInfoArray addObject:dict];
[self savemp3List];
[self resetRecentDocumentsLimit:500];
// use recent Open file to cheat sandbox
NSDocumentController* aa = [NSDocumentController sharedDocumentController];
NSURL* aUrl = [NSURL fileURLWithPath:filename];
[aa noteNewRecentDocumentURL:aUrl];
}
剩下的操作可以根据获得的文件自己操作即可。
Mac下默认的app的recent files是10或者user自己设置的。如果播放歌词会很容易突破这个限制。因此。需要改变一些环境设置。
- (void) resetRecentDocumentsLimit:(int)num
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/defaults"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"write", @"info.popoping.MarmotPlayer", @"NSRecentDocumentsLimit", [NSString stringWithFormat:@"%d", num], nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"returned:\n%@", string);
[string release];
[task release];
}
其中info.popoping.MarmotPlayer是自己的iden,可以换成自己的应用。
我得app已经下架。有需要mp3播放器的朋友可以联系我。邮件:reichtiger@163.com
附上抓图: