Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MAZAudioPlayer.m
- // AudioEngineMac
- //
- #import "MAZAudioPlayer.h"
- #import <AVFoundation/AVFoundation.h>
- @implementation MAZAudioPlayer
- {
- AVAudioEngine *_engine;
- NSError *_engineErr;
- AVAudioPlayerNode *_player;
- AVAudioFile *_file;
- }
- - (instancetype)initWithURL:(NSURL *)fileURL_
- {
- if ((self = [super init]))
- {
- NSURL *url = [[NSBundle mainBundle] URLForResource:@"02 Fire Woman" withExtension:@"mp3"];
- _file = [[AVAudioFile alloc] initForReading:url error:nil];
- _engine = [[AVAudioEngine alloc] init];
- // AVAudioMixerNode *mainMixer = [engine mainMixerNode];
- _player = [[AVAudioPlayerNode alloc] init];
- [_engine attachNode:_player];
- // [_engine connect:_player to:[_engine outputNode] format:_file.processingFormat];
- [_engine connect:_player to:[_engine mainMixerNode] format:_file.processingFormat];
- [_player scheduleFile:_file atTime:nil completionHandler:nil];
- [_engine prepare];
- NSError *engineErr = nil;
- [_engine startAndReturnError:&engineErr];
- _engineErr = engineErr;
- }
- return self;
- }
- - (void)playDuration:(NSTimeInterval)seconds_ looping:(BOOL)looping_
- {
- self.isLooping = looping_;
- if (_engineErr == nil)
- {
- if (seconds_ > 0)
- {
- AVAudioFrameCount count = (_file.fileFormat.sampleRate * seconds_);
- [_player scheduleSegment:_file startingFrame:0 frameCount:count atTime:[[AVAudioTime alloc] initWithSampleTime:(AVAudioFramePosition)0 atRate:_file.fileFormat.sampleRate] completionHandler:^{
- NSLog(@"done");
- }];
- // [_player scheduleSegment:_file startingFrame:0 frameCount:count atTime:[[AVAudioTime alloc] initWithSampleTime:(AVAudioFramePosition)0 atRate:_file.fileFormat.sampleRate] completionHandler:^{
- // NSLog(@"done");
- // }];
- [_player play];
- }
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement