Java程序将amr格式音频转成mp3格式

阅读更多

相信很多人都遇到amr格式的音频文件不能直接在网页播放的问题,有人使用QuickTime插件的辅助,以下介绍如何使用jave.jar包将amr格式的音频文件转成mp3格式,官方下载地址http://www.sauronsoftware.it/projects/jave/download.php,闲话少说,直接上例子

Java代码  收藏代码
  1. package com.nnbrightstar.lbtm.common.utils;  
  2.   
  3. import it.sauronsoftware.jave.AudioAttributes;  
  4. import it.sauronsoftware.jave.Encoder;  
  5. import it.sauronsoftware.jave.EncoderException;  
  6. import it.sauronsoftware.jave.EncodingAttributes;  
  7. import it.sauronsoftware.jave.InputFormatException;  
  8. import java.io.File;  
  9.   
  10. public class ChangeAudioFormat {  
  11.     public static void main(String[] args) throws Exception {  
  12.         String path1 = "E:\\Eclipse_Web\\lbtm\\webapp\\uploadFiles\\1395047224460.amr";  
  13.         String path2 = "E:\\Eclipse_Web\\lbtm\\webapp\\uploadFiles\\1395047224460.mp3";  
  14.         changeToMp3(path1, path2);  
  15.     }  
  16.   
  17.     public static void changeToMp3(String sourcePath, String targetPath) {  
  18.         File source = new File(sourcePath);  
  19.         File target = new File(targetPath);  
  20.         AudioAttributes audio = new AudioAttributes();  
  21.         Encoder encoder = new Encoder();  
  22.   
  23.         audio.setCodec("libmp3lame");  
  24.         EncodingAttributes attrs = new EncodingAttributes();  
  25.         attrs.setFormat("mp3");  
  26.         attrs.setAudioAttributes(audio);  
  27.   
  28.         try {  
  29.             encoder.encode(source, target, attrs);  
  30.         } catch (IllegalArgumentException e) {  
  31.             e.printStackTrace();  
  32.         } catch (InputFormatException e) {  
  33.             e.printStackTrace();  
  34.         } catch (EncoderException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38. }