Last active
April 2, 2016 21:21
-
-
Save brianmpalma/dbf7d5cacdbba5ba29ee4e61c1042703 to your computer and use it in GitHub Desktop.
Minimal Quartz Composer Audio Player Plug In using AVAudioPlayer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Quartz/Quartz.h> | |
@interface BMPAudioFilePlayerPlugIn : QCPlugIn | |
@property (nonatomic, copy) NSString *inputFilePath; | |
@end | |
#import "BMPAudioFilePlayerPlugIn.h" | |
#import <AVFoundation/AVFoundation.h> | |
#define kQCPlugIn_Name @"BMPAudioFilePlayer" | |
#define kQCPlugIn_Description @"BMPAudioFilePlayer description" | |
@interface BMPAudioFilePlayerPlugIn () | |
@property (nonatomic, strong) AVAudioPlayer *audioPlayer; | |
@property (readonly) BOOL isPlaying; | |
@end | |
@implementation BMPAudioFilePlayerPlugIn | |
@dynamic inputFilePath; | |
+ (NSDictionary *)attributes | |
{ | |
return @{QCPlugInAttributeNameKey:kQCPlugIn_Name, QCPlugInAttributeDescriptionKey:kQCPlugIn_Description}; | |
} | |
+ (NSDictionary *)attributesForPropertyPortWithKey:(NSString *)key | |
{ | |
// Specify the optional attributes for property based ports (QCPortAttributeNameKey, QCPortAttributeDefaultValueKey...). | |
if ([key isEqualToString:@"inputFilePath"]) { | |
return @{ | |
QCPortAttributeNameKey: @"File Path", | |
QCPortAttributeDefaultValueKey: @"", | |
}; | |
} else { | |
return nil; | |
} | |
} | |
+ (QCPlugInExecutionMode)executionMode | |
{ | |
return kQCPlugInExecutionModeConsumer; | |
} | |
+ (QCPlugInTimeMode)timeMode | |
{ | |
return kQCPlugInTimeModeNone; | |
} | |
- (AVAudioPlayer *)audioPlayer { | |
if (!_audioPlayer) { | |
NSURL *url = [NSURL URLWithString:self.inputFilePath]; | |
NSError *error = nil; | |
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url fileTypeHint:AVFileTypeAppleM4A error:&error]; | |
if (error) { | |
NSLog(@"%@", error.localizedDescription); | |
} | |
} | |
return _audioPlayer; | |
} | |
@end | |
@implementation BMPAudioFilePlayerPlugIn (Execution) | |
- (BOOL)startExecution:(id <QCPlugInContext>)context | |
{ | |
return YES; | |
} | |
- (BOOL)execute:(id <QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary *)arguments | |
{ | |
if (!self.isPlaying) { | |
[self startPlaying]; | |
} | |
return YES; | |
} | |
- (void)disableExecution:(id <QCPlugInContext>)context | |
{ | |
[self stopPlaying]; | |
} | |
- (void)stopExecution:(id <QCPlugInContext>)context | |
{ | |
[self stopPlaying]; | |
} | |
- (BOOL)isPlaying { | |
return self.audioPlayer.isPlaying; | |
} | |
- (void)startPlaying { | |
[self.audioPlayer play]; | |
} | |
- (void)stopPlaying { | |
[self.audioPlayer stop]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment