Advertisement
Guest User

Untitled

a guest
Nov 14th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  MAZAudioPlayer.m
  3. //  AudioEngineMac
  4. //
  5.  
  6. #import "MAZAudioPlayer.h"
  7. #import <AVFoundation/AVFoundation.h>
  8.  
  9. @implementation MAZAudioPlayer
  10. {
  11.     AVAudioEngine *_engine;
  12.     NSError *_engineErr;
  13.     AVAudioPlayerNode *_player;
  14.     AVAudioFile *_file;
  15. }
  16.  
  17. - (instancetype)initWithURL:(NSURL *)fileURL_
  18. {
  19.     if ((self = [super init]))
  20.     {
  21.         NSURL *url = [[NSBundle mainBundle] URLForResource:@"02 Fire Woman" withExtension:@"mp3"];
  22.         _file = [[AVAudioFile alloc] initForReading:url error:nil];
  23.         _engine = [[AVAudioEngine alloc] init];
  24.         //    AVAudioMixerNode *mainMixer = [engine mainMixerNode];
  25.         _player = [[AVAudioPlayerNode alloc] init];
  26.         [_engine attachNode:_player];
  27. //        [_engine connect:_player to:[_engine outputNode] format:_file.processingFormat];
  28.         [_engine connect:_player to:[_engine mainMixerNode] format:_file.processingFormat];
  29.         [_player scheduleFile:_file atTime:nil completionHandler:nil];
  30.         [_engine prepare];
  31.        
  32.         NSError *engineErr = nil;
  33.         [_engine startAndReturnError:&engineErr];
  34.         _engineErr = engineErr;
  35.     }
  36.     return self;
  37. }
  38.  
  39. - (void)playDuration:(NSTimeInterval)seconds_ looping:(BOOL)looping_
  40. {
  41.     self.isLooping = looping_;
  42.     if (_engineErr == nil)
  43.     {
  44.         if (seconds_ > 0)
  45.         {
  46.             AVAudioFrameCount count = (_file.fileFormat.sampleRate * seconds_);
  47.             [_player scheduleSegment:_file startingFrame:0 frameCount:count atTime:[[AVAudioTime alloc] initWithSampleTime:(AVAudioFramePosition)0 atRate:_file.fileFormat.sampleRate] completionHandler:^{
  48.                 NSLog(@"done");
  49.             }];
  50. //            [_player scheduleSegment:_file startingFrame:0 frameCount:count atTime:[[AVAudioTime alloc] initWithSampleTime:(AVAudioFramePosition)0 atRate:_file.fileFormat.sampleRate] completionHandler:^{
  51. //                NSLog(@"done");
  52. //            }];
  53.             [_player play];
  54.         }
  55.     }
  56. }
  57.  
  58. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement