今天一个录制音频到服务端的功能
音频录制
导入头文件
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
两个对象
@property (nonatomic, strong) AVAudioRecorder *audioRecorder;// 录音对象
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;// 播放对象
@property (nonatomic ,strong) NSURL *recorderUrl;// acf本地文件url
@property (nonatomic ,strong) NSURL *mp3Url;// MP3 文件url
//设置音频会话
AVAudioSession *session = [AVAudioSession sharedInstance];// 获取音频会话的实例
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];// 设置音频会话类别
if (session == nil) {
NSLog(@"创建音频会话失败 %@",[sessionError description]);
}else{
[session setActive:YES error:nil];
}
_recorderUrl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"这里自己写文件名称就行"]];
NSMutableDictionary *recordict = [NSMutableDictionary dictionary];
[recordict setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordict setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];
[recordict setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordict setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordict setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
_audioRecorder = [[AVAudioRecorder alloc] initWithURL:_recorderUrl settings:recordict error:nil];
_audioRecorder.delegate = self;
_audioRecorder.meteringEnabled = YES;
[_audioRecorder prepareToRecord];
[_audioRecorder record];
_audioPlayer = nil;
录制成功以后播放本地音频
[_audioRecorder stop];
_audioRecorder = nil;
NSError *playerError;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_recorderUrl error:&playerError];
if (_audioPlayer == nil) {
NSLog(@"创建录音失败 %@",[_audioPlayer description]);
}
_audioPlayer.delegate = self;
NSLog(@"播放录音");
if ([_audioPlayer isPlaying]) {// 如果在播放状态下
[_audioPlayer pause];
}else{
NSError *playerError;
if (_audioPlayer == nil) {
NSLog(@"创建录音失败 %@",[playerError description]);
}
NSLog(@"正在播放");
[_audioPlayer play];
}
到这里 播放没问题 就上传
[_audioRecorder stop];
_audioRecorder = nil;
[_audioPlayer stop];
这里转MP3
需要导入 lame静态库 这个转MP3
参考文章:https://www.jianshu.com/p/c1bdab0ddf59
https://www.jianshu.com/p/d0645a6d8834
这里参考上面文章
上传到服务端
// 上传音频
+ (void)uploadRecordWithURLString:(NSString *)URLString
parameters:(id)parameters
uploadData:(NSData *)uploadData
uploadName:(NSString *)uploadName
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
#pragma mark -- 上传音频 --
这里data
NSData *data = [[NSData alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", @"Mp3File.mp3"]];//就是本地存的MP3 文件
+ (void)uploadRecordWithURLString:(NSString *)URLString parameters:(id)parameters uploadData:(NSData *)uploadData uploadName:(NSString *)uploadName success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
NSString *urlStr = [NSString stringWithFormat:@"%@%@",NETWORKINGURL,URLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"] forHTTPHeaderField:@"token"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *string = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.mp3",string];
[manager POST:urlStr parameters:parameters headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:uploadData name:uploadName fileName:fileName mimeType:@"application/octet-stream"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
搞定 记录下来
标签:audioRecorder,nil,audioPlayer,manager,caf,NSString,MP3,音频,服务端 From: https://www.cnblogs.com/xiaodeng90/p/17387549.html