spark项目中需要实现类似微信的语音聊天功能,主体流程:
流程 写道
1.终端将要发送的语音存储到本地,然后将对应的语音发送到资源服务器。获取网络地址
2.接收端在接收的时候,需将音频下载缓存到本地
3.读取本地缓存,对音频解码播放
2.接收端在接收的时候,需将音频下载缓存到本地
3.读取本地缓存,对音频解码播放
在实现的过程,发现android端发送的amr格式,在spark电脑无法解析。需要将amr转换为wav格式才能正常解析。
public static File convert(String oldFilePath,String newFilePath,String voiceType){
File target = new File(newFilePath);
try {
File source = new File(oldFilePath);
AudioAttributes audio = new AudioAttributes();
audio.setBitRate(new Integer(128000));
audio.setChannels(new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(voiceType);
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
// 将旧数据删除
if (source.exists()){
source.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return target;
}
使用上面的代码需要依赖jave-1.0.2.jar!
参数说明:
写道
oldFilePath:原本音频地址
newFilePath:需要转换后的音频地址
voiceType:需要转换的音频类型(如:wav,mp3等)
newFilePath:需要转换后的音频地址
voiceType:需要转换的音频类型(如:wav,mp3等)
按照上面的步骤即可解析音频文件。