-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioRecorder.m
108 lines (87 loc) · 3.03 KB
/
AudioRecorder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// AudioRecorder.m
// ActionTap
//
// Created by Ryan Sullivan on 3/28/15.
// Copyright (c) 2015 Sebastian Cain. All rights reserved.
//
#import "AudioRecorder.h"
@implementation AudioRecorder
- (instancetype) init {
return [self initWithSongName: @"Default"];
}
- (instancetype) initWithSongName: (NSString *)songName {
self = [super init];
if (self){
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", songName,@".caf"]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt:1],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];
_audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[_audioRecorder prepareToRecord];
_audioRecorder.meteringEnabled = YES;
}
}
return self;
}
- (void)recordAudio{
if (!_audioRecorder.recording)
{
self.recording = [_audioRecorder record];
}
}
- (void)stopAudio {
if (_audioRecorder.recording)
{
[_audioRecorder stop];
self.recording = NO;
}
}
-(void)pauseUnpause
{
if (_audioRecorder.recording) {
[_audioRecorder pause];
self.recording = NO;
}else{
self.recording = [_audioRecorder record];
}
}
-(NSURL*)getSongURL{
return _audioRecorder.url;
}
-(float)getVolume{
[_audioRecorder updateMeters];
return [_audioRecorder averagePowerForChannel:0];
}
//Returns the amount of time that has passed in seconds, with millisecond precision
//NSTimeInterval is a typealiased double
-(NSTimeInterval)getTimeSinceStart{
return [_audioRecorder currentTime];
}
@end