Jump to content

[Java] MP3 to WAV compression

Hey guys I was just wondering if you had a way of compressing MP3 to WAV in java as I'm trying to play a MP3 track in java. I'm currently using the JLayer library which works PERFECTLY fine in windows but it doesn't appear to be supported in linux. so is there some sort of way to compress MP3 into WAV in java to play the tracks? I don't want to manually compress them as the compression ratio is to high, when compressing it manually the filesize increase by around 90% which will obviously make the JAR to big. I'd consider myself to be a intermediate programmer so feel free to get a little overboard and use advanced methods as I more then likely might be able to understand them, this doesn't mean that I'll be understanding everything so do expect me to ask "what does this mean".

Link to comment
Share on other sites

Link to post
Share on other sites

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){                }            }        }    }

I found this here it was the awnser. I don't program Java myself but i hope it helped! 

[CPU: AMD FX-6100 @3.3GHz ] [MoBo: Asrock 970 Extreme4] [GPU: Gigabyte 770 OC ] [RAM: 8GB] [sSD: 64gb for OS] [PSU: 550Watt Be Quiet!] [HDD: 1TB] [CPU cooler: Be Quiet! Shadow Rock Pro Sr1]  -Did i solve your question/problem? Please click 'Marked Solved'-

Link to comment
Share on other sites

Link to post
Share on other sites

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){                }            }        }    }

I found this here it was the awnser. I don't program Java myself but i hope it helped! 

 

that makes use of JLayer which I'm currently using and what I'm looking for an alternative which does not really make use of any sort of libraries other then the stock JRE libraries since JLayer isn't supported by linux at least it doesn't work with linux

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×