Skip to content

Commit

Permalink
Tore out NSSound in favor of QTMovie. App is now using QTMovie common…
Browse files Browse the repository at this point in the history
…Metadata to find out title and artist
  • Loading branch information
kallepersson committed Feb 15, 2012
1 parent 64f7a7e commit 6460021
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
10 changes: 6 additions & 4 deletions AppDelegate.h
Expand Up @@ -7,6 +7,7 @@
//

#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
#import "INAppStoreWindow.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>
Expand All @@ -21,11 +22,12 @@


@property (assign) BOOL paused;
@property (assign) double startTime;
@property (assign) double endTime;
@property (assign) double currentTime;
@property (assign) QTTime startTime;
@property (assign) QTTime endTime;
@property (assign) QTTime currentTime;
@property (assign) long timeScale;

@property (retain) NSSound *music;
@property (retain) QTMovie *music;
- (void)checkTime:(NSTimer*)theTimer;
- (IBAction)playButtonClick:(id)sender;
- (IBAction)startSliderSet:(id)sender;
Expand Down
86 changes: 52 additions & 34 deletions AppDelegate.m
Expand Up @@ -8,6 +8,7 @@

#import "AppDelegate.h"
#import <CoreAudio/CoreAudio.h>
#import <QTKit/QTKit.h>
#import "metadataRetriever.h"

@implementation AppDelegate
Expand All @@ -20,6 +21,7 @@ @implementation AppDelegate
@synthesize playButton;
@synthesize currentTrackLabel;

@synthesize timeScale;
@synthesize startTime;
@synthesize endTime;
@synthesize currentTime;
Expand All @@ -35,82 +37,100 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

-(void) checkTime:(NSTimer*)theTimer{
currentTime = [music currentTime];
if([music isPlaying]){
if(currentTime >= endTime && startTime < endTime){
[music setCurrentTime:startTime];
}

if(currentTime.timeValue >= endTime.timeValue && startTime.timeValue < endTime.timeValue){
[music setCurrentTime:startTime];
}


NSCalendar *sysCalendar = [NSCalendar currentCalendar];

NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:currentTime sinceDate:date1];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:currentTime.timeValue/timeScale sinceDate:date1];

unsigned int unitFlags = NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];

[currentTimeLabel setStringValue:[NSString stringWithFormat:@"%02d:%02d",[conversionInfo minute],[conversionInfo second]]];
[currentTimeBar setFloatValue:currentTime];
[currentTimeBar setFloatValue:(float)currentTime.timeValue];

}

- (void)loadMusic:(NSURL *) fileURL {
NSSound * m = [NSSound alloc];
music = [m initWithContentsOfURL:fileURL byReference:YES];
double maxValue = [music duration];

NSString * trackFilePath = [fileURL path];
NSArray *metadataArray = [metadataRetriever getMetadataForFile:trackFilePath];
[currentTrackLabel setStringValue:[NSString stringWithFormat:@"%@\n%@",[metadataArray objectAtIndex:1],[metadataArray objectAtIndex:0]]];
//Load the track from URL
//TODO: Error handling
music = [[QTMovie alloc] initWithURL:fileURL error:nil];



//Really needed anymore?
paused = YES;
startTime = 0.0;
endTime = maxValue;
[currentTimeBar setMaxValue:endTime];

//Find and set slider max values
QTTime maxTime = [music duration];
timeScale = [music duration].timeScale;
float maxValue = (float)maxTime.timeValue;
startTime = QTMakeTime(0.0,timeScale);
endTime = maxTime;

[currentTimeBar setMaxValue:maxValue];
[startSlider setMaxValue:maxValue];
[startSlider setFloatValue:0.0];
[endSlider setMaxValue:maxValue];
[endSlider setFloatValue:maxValue];
[startSlider setNumberOfTickMarks:(int) endTime];
[endSlider setNumberOfTickMarks:(int) endTime];
[startSlider setNumberOfTickMarks:(int) maxValue/timeScale];
[endSlider setNumberOfTickMarks:(int) maxValue/timeScale];

//Set title and artist labels from metadata
NSArray * mdArray = [music commonMetadata];
NSString * trackTitle = @"Unknown title";
NSString * trackArtist = @"Unknown artist";

NSArray * titleMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:@"title" keySpace:nil];
if([titleMetadataItems count] > 0) {
trackTitle = [[titleMetadataItems objectAtIndex:0] stringValue];
}
NSArray * artistMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:@"artist" keySpace:nil];
if([artistMetadataItems count] > 0) {
trackArtist = [[artistMetadataItems objectAtIndex:0] stringValue];
}

[currentTrackLabel setStringValue:[NSString stringWithFormat:@"%@\n%@",trackTitle,trackArtist]];

//Start loop and play track
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkTime:) userInfo:nil repeats:YES];
[music play];
[music pause];
}

- (IBAction)startSliderSet:(id)sender {
if([startSlider doubleValue] < endTime) {
startTime = [startSlider doubleValue];
if([startSlider doubleValue] < (float)endTime.timeValue) {
startTime = QTMakeTime((long)[startSlider doubleValue],timeScale);

}
else{
[startSlider setFloatValue:startTime];
[startSlider setFloatValue:(float)startTime.timeValue];
}
}

- (IBAction)endSliderSet:(id)sender {
if([endSlider doubleValue] > startTime) {
endTime = [endSlider doubleValue];
if([endSlider doubleValue] > (float)startTime.timeValue) {
endTime = QTMakeTime((long)[endSlider doubleValue],timeScale);
}
else{
[endSlider setFloatValue:endTime];
[endSlider setFloatValue:(float)endTime.timeValue];
}
}

- (IBAction)currentTimeBarSet:(id)sender {
NSTimeInterval ct = [currentTimeBar doubleValue];
[music setCurrentTime:ct];
[music setCurrentTime:QTMakeTime((long)ct,timeScale)];
}

- (IBAction)playButtonClick:(id)sender {
if(!paused) {
[music pause];
[music stop];
paused = YES;
}
else {
[music resume];
[music play];
paused = NO;
}
}
Expand All @@ -119,9 +139,7 @@ - (IBAction)openFile:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSInteger tvarNSInteger = [openPanel runModal];
if(tvarNSInteger == NSOKButton){
if([music isPlaying]) {
[music stop];
}
[music stop];
NSURL * fileURL = [openPanel URL];
[self loadMusic:fileURL];
}
Expand Down
4 changes: 4 additions & 0 deletions Perpetual.xcodeproj/project.pbxproj
Expand Up @@ -19,6 +19,7 @@
9AC739E314EAEAB6000A57AD /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 9AC739E114EAEAB6000A57AD /* Credits.rtf */; };
9AC739E614EAEAB6000A57AD /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AC739E514EAEAB6000A57AD /* AppDelegate.m */; };
9AC739E914EAEAB6000A57AD /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9AC739E714EAEAB6000A57AD /* MainMenu.xib */; };
9AE4EA5714EC59D6002EF094 /* QTKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9AE4EA5614EC59D6002EF094 /* QTKit.framework */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -42,6 +43,7 @@
9AC739E414EAEAB6000A57AD /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
9AC739E514EAEAB6000A57AD /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
9AC739E814EAEAB6000A57AD /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainMenu.xib; sourceTree = "<group>"; };
9AE4EA5614EC59D6002EF094 /* QTKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QTKit.framework; path = System/Library/Frameworks/QTKit.framework; sourceTree = SDKROOT; };
DDB3CDEF03204F9F8C7160C4 /* Pods.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

Expand All @@ -50,6 +52,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9AE4EA5714EC59D6002EF094 /* QTKit.framework in Frameworks */,
9A31AA3414EBF3FD00382B2F /* AudioToolbox.framework in Frameworks */,
9395B6AD14EB88A8002999D9 /* QuartzCore.framework in Frameworks */,
9AC739D314EAEAB6000A57AD /* Cocoa.framework in Frameworks */,
Expand All @@ -63,6 +66,7 @@
9AC739C314EAEAB6000A57AD = {
isa = PBXGroup;
children = (
9AE4EA5614EC59D6002EF094 /* QTKit.framework */,
9A31AA3314EBF3FD00382B2F /* AudioToolbox.framework */,
9395B6AC14EB88A8002999D9 /* QuartzCore.framework */,
9AC739D814EAEAB6000A57AD /* Perpetual */,
Expand Down

0 comments on commit 6460021

Please sign in to comment.