-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioController.m
170 lines (142 loc) · 4.3 KB
/
AudioController.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// AudioController.m
// grapevine
//
// Created by Ian Gillis on 9/3/12.
// Copyright (c) 2012 Ian Gillis. All rights reserved.
//
#import "AudioController.h"
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
@implementation AudioController
@synthesize recordingPath;
static AudioController* _sharedInstance = nil;
static AVAudioPlayer* _audioPlayer = nil;
static AVAudioRecorder* _audioRecorder = nil;
static AVPlayer* _audioStreamer = nil;
+(AudioController*) sharedInstance {
if (_sharedInstance == nil) {
_sharedInstance = [[self alloc] init];
}
return _sharedInstance;
}
-(void) prepareToPlay {
[_audioPlayer prepareToPlay];
}
-(int) lengthOfCurrentTrack {
if (_audioPlayer && _audioPlayer.isPlaying) {
return _audioPlayer.duration;
}
return 0;
}
-(void) playAudio:(NSData *)data {
if (!(_audioPlayer && [_audioPlayer.data isEqualToData:data])) {
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[_audioPlayer setVolume:1.0];
}
[_audioPlayer play];
}
-(void) playAudioFromFile:(NSString *)file {
NSURL* url = [NSURL fileURLWithPath:file];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_audioPlayer setVolume:1.0];
[_audioPlayer play];
}
-(void) pauseAudio {
if (_audioPlayer && [_audioPlayer isPlaying]) {
[_audioPlayer pause];
}
}
//newTime represents the time to start playback at
// in seconds (as we only have 30 secs of audio)
-(BOOL) setTime: (int) newTime {
[_audioPlayer setCurrentTime:newTime];
if (![_audioPlayer isPlaying]) {
[_audioPlayer play];
}
return YES;
}
-(void) stopAudio {
if (_audioPlayer) {
[_audioPlayer stop];
_audioPlayer = nil;
}
}
-(BOOL) isPlaying {
if (_audioPlayer) {
return [_audioPlayer isPlaying];
}
if (_audioStreamer) {
return _audioStreamer.rate > 0.0;
}
return NO;
}
-(NSData*) currentlyPlayingTrack {
if (_audioPlayer) {
return _audioPlayer.data;
}
return nil;
}
-(BOOL) isRecording {
return _audioRecorder.isRecording;
}
-(void) beginRecording {
[_audioRecorder record];
}
-(void) stopRecording {
[_audioRecorder stop];
}
-(void) pauseStreamedAudio {
[_audioStreamer pause];
}
-(void) prepareToStream:(NSString *)url {
NSURL* urlObj = [NSURL URLWithString:url];
_audioStreamer = [[AVPlayer alloc] initWithURL:urlObj];
}
-(void) playStreamedAudio {
[_audioStreamer play];
}
-(void) stopStreamedAudio {
[_audioStreamer pause];
_audioStreamer = nil;
}
-(BOOL) isStreaming {
return YES;
}
-(double) lengthOfStreamingTrack {
return CMTimeGetSeconds(_audioStreamer.currentItem.duration);
}
-(AudioController*) init {
self = [super init];
if (self) {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[paths objectAtIndex:0];
recordingPath =[documentsDir stringByAppendingPathComponent:@"mysound.aiff"];
NSURL* recordingURL = [[NSURL alloc] initFileURLWithPath: recordingPath];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMedium],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
_audioRecorder = [[AVAudioRecorder alloc]
initWithURL:recordingURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
NSLog(@"%@", _audioRecorder.url);
[_audioRecorder prepareToRecord];
}
}
return self;
}
@end