简单介绍:
AudioRecord录音
主要是实现边录边播(AudioRecord+AudioTrack)以及对音频的实时处理(如会说话的汤姆猫、语音)。 所以就用这个录WAV文件。这样录的文件是未经过压缩的所以,文件必要大,但是听起来也比较清晰,也支持很多其他的跨平台设备
MediaRecorder录音
已经集成了录音、编码、压缩等,支持少量的录音音频格式,大概有.aac(API = 16) .amr .3gp。 使用非常简单,MediaRecorder录音,文件小,音频是经过压缩的,所以听起来,可能效果不太好
使用Retrofit2上传文件(修改用户其他数据,并同时上传文件)
请求链接格式
@POST("ifc/saveChildCfgMsgSos.html") @Multipart Observable<BaseResp> saveChildCfgMsgSos(@Query("content") String content,@Part MultipartBody.Part file);
请求方法,上传wav格式音频
public void saveChildCfgMsgSos(final String json, final String path) { Observable.just("") .subscribeOn(Schedulers.io()) // 切换至IO线程 .flatMap(new Func1<String, Observable<BaseResp>>() { @Override public Observable<BaseResp> call(String s) { // 封装请求体 File file = new File(path); RequestBody requestFile = RequestBody.create(MediaType.parse("audio/wav"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile); return getApiClient().saveChildCfgMsgSos(json, body); } }).observeOn(AndroidSchedulers.mainThread()) // 切换至Android主线程 .subscribe(new Subscriber<BaseResp>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Log.e("saveChildCfgMsgSos onError", "" + e.getMessage()); postEvent(new FailedEvent(e.getMessage())); } @Override public void onNext(BaseResp resp) { postEvent(resp); } }); }
上传异常404异常
检查是否是Retrofit2网络框架库太低
compile 'io.reactivex:rxjava:1.2.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'