iOS 语音上传 语音录制 上传 播放

2017-11-16  本文已影响0人  JumpSmallFlame

语音上传 语音录制 上传 播放按钮
[self.speakBtn addTarget:self action:@selector(speakClick:forEvent:) forControlEvents:UIControlEventAllTouchEvents];

pragma mark-录音

 [self.speakBtn setBackgroundImage:[UIImage imageNamed:@"qx10"] forState:UIControlStateNormal];

}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
URL = recorder.url;
data = [NSData dataWithContentsOfURL:URL];
[self update];
NSLog(@"%@",URL);
}

/说话

}

pragma mark:上传语音

-(void)update{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil ];
[manager POST:[NSString stringWithFormat:@"http://tyg.youruide.com/app/%@",updateYuYinUrl] parameters:@{} constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];//这里面没做优化,建议用一个单例
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.mp3", str];//文件名

    [formData appendPartWithFileData:data name:@"audio" fileName:fileName mimeType:@".mp3"];//name:file与服务器对应
} progress:^(NSProgress * _Nonnull uploadProgress) {
    
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSInteger code = [responseObject [@"code"]integerValue];
    if (code == 200) {
        
        filepath = [[responseObject objectForKey:@"data"] objectForKey:@"filepath"];
        
        [self showSuccessMessage:[responseObject  objectForKey:@"message"]];
        
        
    }
    NSLog(@"上传成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
   
    NSLog(@"上传错误");
}];

}

上一篇下一篇

猜你喜欢

热点阅读