-
Notifications
You must be signed in to change notification settings - Fork 10
/
Voice.mm
169 lines (143 loc) · 3.51 KB
/
Voice.mm
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
//
// Voice.m
// TestRecord
//
// Created by LennonChen on 16/2/26.
// Copyright © 2016年 LennonChen. All rights reserved.
//
#import "Voice.h"
#import "VoiceConverter.h"
static AVAudioRecorder *recorder = nil;
static AVAudioPlayer *player = nil;
@implementation VoiceRecorder
+ (int)Start:(NSString *)file
{
NSURL *fileUri = [NSURL fileURLWithPath: file];
NSError *error = nil;
recorder = [[AVAudioRecorder alloc] initWithURL: fileUri settings: [VoiceConverter GetAudioRecorderSettingDict] error:&error];
if (error)
{
NSLog(@"录音机创建失败:%@", error.localizedDescription);
return 0;
}
error = nil;
if ([recorder prepareToRecord])
{
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &error];
[[AVAudioSession sharedInstance] setActive: YES error: &error];
if (error)
{
NSLog(@"录音预处理失败:%@", error.localizedDescription);
return 0;
}
}
if ([recorder record])
{
return 1;
}
return 0;
}
+ (void)Stop
{
if (recorder && recorder.isRecording)
{
[recorder stop];
recorder = nil;
}
if (player && player.isPlaying)
{
[player stop];
player = nil;
}
}
+ (int)Play:(NSString *)file
{
NSError *error = nil;
NSURL *fileUri = [NSURL fileURLWithPath: file];
player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileUri error:&error];
if (error)
{
NSLog(@"播放机错误:%@", error.localizedDescription);
return 0;
}
player.numberOfLoops = 0;
[player prepareToPlay];
if ([player play])
{
return 1;
}
return 0;
}
+ (int)IsPlaying
{
if (player && player.isPlaying)
{
return 1;
}
return 0;
}
+ (int)IsRecording
{
if (recorder && recorder.isRecording)
{
return 1;
}
return 0;
}
+ (NSString *)GetCurrentTimeForString
{
NSDate *date = [NSDate dateWithTimeIntervalSinceNow: 0];
NSTimeInterval a = [date timeIntervalSince1970] * 1000;
NSString *time = [NSString stringWithFormat: @"%f", a];
return time;
}
+ (unsigned long long)GetFileSize:(NSString *)filePath
{
NSFileManager *mgr = [NSFileManager defaultManager];
if ([mgr fileExistsAtPath: filePath])
{
unsigned long long size = [[mgr attributesOfItemAtPath: filePath error:nil] fileSize];
return size;
}
return -1;
}
+ (int)RenameFile:(NSString *)orgFile toPath:(NSString *)dstPath
{
NSFileManager *mgr = [NSFileManager defaultManager];
if ([mgr fileExistsAtPath: orgFile])
{
if ([mgr copyItemAtPath: orgFile toPath: dstPath error: nil])
{
[mgr removeItemAtPath: orgFile error: nil];
return 1;
}
}
return 0;
}
+ (int)WAV2AMR:(NSString *)wavFile amr:(NSString *)amrFile
{
if ([VoiceConverter ConvertWavToAmr: wavFile amrSavePath: amrFile])
{
unsigned long long size = [VoiceRecorder GetFileSize: amrFile];
if (size > 0)
{
return 1;
}
}
NSLog(@"WAV转换AMR失败!");
return 0;
}
+ (int)AMR2WAV:(NSString *)amrFile wav:(NSString *)wavFile
{
if ([VoiceConverter ConvertAmrToWav: amrFile wavSavePath: wavFile])
{
unsigned long long size = [VoiceRecorder GetFileSize: wavFile];
if (size > 0)
{
return 1;
}
}
NSLog(@"AMR转换WAV失败!");
return 0;
}
@end