html5中audio支持音频格式
-
HTML5 Audio标签能够支持wav, mp3, ogg, acc, webm等格式
-
但有个很重要的音乐文件格式amr(扩展名amr)却在各大浏览器中都没有内置的支持。
音频格式 | Chrome | Firefox | IE9 | Opera | Safari |
---|---|---|---|---|---|
OGG | 支持 | 支持 | 支持 | 不支持 | 不支持 |
MP3 | 支持 | 不支持 | 支持 | 不支持 | 支持 |
WAV | 不支持 | 支持 | 不支持 | 支持 | 不支持 |
Java AMR格式音频转MP3
-
pom
-
ws.schild(Windows版本、Linux版本)
-
<!-- 文件格式转换依赖--> <!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>2.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-mock --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-mock</artifactId> <version>2.0.8</version> </dependency> <!-- https://mvnrepository.com/artifact/ws.schild/jave-native-win64 Windows版本--> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-win64</artifactId> <version>2.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/ws.schild/jave-native-linux64 Linux版本--> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-linux64</artifactId> <version>2.4.4</version> </dependency>
-
-
properties
-
filePath: sourcePath: /tmp/matter/tempFilePath/sourcePath/ uploadPath: /tmp/matter/tempFilePath/uploadPath/
-
-
class工具类
-
package com.tqservice.web.core.utils; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import ws.schild.jave.AudioAttributes; import ws.schild.jave.Encoder; import ws.schild.jave.EncodingAttributes; import ws.schild.jave.MultimediaObject; import java.io.*; /** * 文件格式转换工具 */ @Component public class FileFormat { @Value("${filePath.sourcePath}") private String AMRLOCALPATH; @Value("${filePath.uploadPath}") private String MP3LOCALPATH; /** * 上传文件下载到本地 * * @param multipartFile */ public String downloadFile(MultipartFile multipartFile) { OutputStream os = null; InputStream inputStream = null; String fileName = null; String fullPath = null; try { inputStream = multipartFile.getInputStream(); fileName = multipartFile.getOriginalFilename(); } catch (IOException e) { e.printStackTrace(); } try { // 保存到临时文件 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流保存到本地文件,判断文件夹是否存在 File tempFile = new File(AMRLOCALPATH); if (!tempFile.exists()) { tempFile.mkdirs(); } os = new FileOutputStream(tempFile.getPath() + File.separator + fileName); // 开始读取 while ((len = inputStream.read(bs)) != -1) { os.write(bs, 0, len); } fullPath = AMRLOCALPATH + multipartFile.getOriginalFilename(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 完毕,关闭所有链接 try { os.close(); inputStream.close(); return fullPath; } catch (IOException e) { e.printStackTrace(); return fullPath; } } } /** * 文件格式转换 * * @param multipartFile 目标文件 * @return */ public MultipartFile formatConversion(MultipartFile multipartFile) { MockMultipartFile mockMultipartFile = null; try { //转码源文件 String sourcePath = downloadFile(multipartFile); if (null == sourcePath) { return mockMultipartFile; } File source = new File(sourcePath); //判断文件夹是否存在 File file = new File(MP3LOCALPATH); if (!file.exists()) { file.mkdirs(); } //转码本地输出文件 String saveFilePath = multipartFile.getOriginalFilename(); saveFilePath = MP3LOCALPATH + saveFilePath.substring(0, saveFilePath.lastIndexOf(".") + 1) + "mp3"; File target = new File(saveFilePath); //Audio Attributes AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(128000); audio.setChannels(2); audio.setSamplingRate(44100); //Encoding attributes EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); //Encode Encoder encoder = new Encoder(); encoder.encode(new MultimediaObject(source), target, attrs); //转MultipartFile FileInputStream fileInputStream = new FileInputStream(target); mockMultipartFile = new MockMultipartFile(target.getName(), fileInputStream); } catch (Exception ex) { ex.printStackTrace(); } return mockMultipartFile; } /** * 删除临时文件 * * @param fileName */ public void deleteTempFile(String fileName) { String amrFilePath = AMRLOCALPATH + fileName.substring(0, fileName.lastIndexOf(".") + 1) + "amr"; String mp3FilePath = MP3LOCALPATH + fileName; File amrFile = new File(amrFilePath); File mp3File = new File(mp3FilePath); if (amrFile.isFile() && amrFile.exists()) { //存在则进行删除 amrFile.delete(); } if (mp3File.isFile() && mp3File.exists()) { mp3File.delete(); } } }
-