java audio type conversion (spark chat)

In the spark project, a voice chat function similar to WeChat needs to be implemented. The main process is as follows:

Process writes
1. The terminal stores the voice to be sent locally, and then sends the corresponding voice to the resource server. Get the network address

2. When the receiver is receiving, it needs to cache the audio download to the local

3. Read the local cache, decode and play the audio

 

During the implementation process, it was found that the amr format sent by the android side could not be parsed by the spark computer. Need to convert amr to wav format for normal parsing.

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);
			
			// delete old data
			if (source.exists()){
				source.delete();
			}
		} catch (Exception e) {
			e.printStackTrace ();
		}
		
		return target;
		
	}

 Using the above code needs to depend on jave-1.0.2.jar!

Parameter Description:

wrote
oldFilePath: Original audio address
newFilePath: Audio address to be converted
voiceType: Audio type to be converted (eg: wav, mp3, etc.)

 

Follow the steps above to parse the audio file.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326808687&siteId=291194637