栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中将mp3转换为WAV

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中将mp3转换为WAV

public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception{        if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){ throw new IllegalArgumentException("Illegal Argument passed to this method");        }        ByteArrayInputStream bais = null;        ByteArrayOutputStream baos = null;        AudioInputStream sourceAIS = null;        AudioInputStream convert1AIS = null;        AudioInputStream convert2AIS = null;        try{ bais = new ByteArrayInputStream(sourceBytes); sourceAIS = AudioSystem.getAudioInputStream(bais); AudioFormat sourceFormat = sourceAIS.getFormat(); AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false); convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS); baos = new ByteArrayOutputStream(); byte [] buffer = new byte[8192]; while(true){     int readCount = convert2AIS.read(buffer, 0, buffer.length);     if(readCount == -1){         break;     }     baos.write(buffer, 0, readCount); } return baos.toByteArray();        } catch(UnsupportedAudioFileException uafe){ //uafe.printStackTrace(); throw uafe;        } catch(IOException ioe){ //ioe.printStackTrace(); throw ioe;        } catch(IllegalArgumentException iae){ //iae.printStackTrace(); throw iae;        } catch (Exception e) { //e.printStackTrace(); throw e;        }finally{ if(baos != null){     try{         baos.close();     }catch(Exception e){     } } if(convert2AIS != null){     try{         convert2AIS.close();     }catch(Exception e){     } } if(convert1AIS != null){     try{         convert1AIS.close();     }catch(Exception e){     } } if(sourceAIS != null){     try{         sourceAIS.close();     }catch(Exception e){     } } if(bais != null){     try{         bais.close();     }catch(Exception e){     } }        }    }

这里的sourceBytes表示MP3文件或WAV文件。audioFormat是您要转换的PCM格式。另外,我们需要将javazoom.com中的mp3spi.jar,tritonus_mp3.jar,jl* .jar,tritonus_share.jar放入类路径中。希望这对其他人有帮助。

Java 7版本:

public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception {    if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){        throw new IllegalArgumentException("Illegal Argument passed to this method");    }    try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes);         final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {        AudioFormat sourceFormat = sourceAIS.getFormat();        AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false);        try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS);  final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS);  final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte [] buffer = new byte[8192]; while(true){     int readCount = convert2AIS.read(buffer, 0, buffer.length);     if(readCount == -1){         break;     }     baos.write(buffer, 0, readCount); } return baos.toByteArray();        }    }}

Maven:

<dependency>    <groupId>com.googlepre.soundlibs</groupId>    <artifactId>mp3spi</artifactId>    <version>1.9.5-1</version></dependency><dependency>    <groupId>com.googlepre.soundlibs</groupId>    <artifactId>jlayer</artifactId>    <version>1.0.1-1</version>    <exclusions>        <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId>        </exclusion>    </exclusions></dependency>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号