Skip to content

code to create mp3 convertor #30

Description

@wendyjuly
No description provided.

Activity

  1. wendyjuly commented on Apr 17, 2017

    @wendyjuly
    Author

    Anyone?

  2. sanjeeth-07 commented on Apr 17, 2017

    @sanjeeth-07
  3. fwx412269 commented on Aug 29, 2019

    @fwx412269

    thanks for share.

  4. sanjeet28 commented on Dec 12, 2023

    @sanjeet28

    hi Wendy,
    see the my code of converting mp3 convertor
    import javazoom.jl.decoder.Bitstream;
    import javazoom.jl.decoder.JavaLayerException;
    import javazoom.jl.player.advanced.AdvancedPlayer;
    import javazoom.jl.player.advanced.PlaybackEvent;
    import javazoom.jl.player.advanced.PlaybackListener;

    import javax.sound.sampled.*;

    public class MP3Converter {

    public static void main(String[] args) {
        convertMP3ToWAV("input.mp3", "output.wav");
    }
    
    public static void convertMP3ToWAV(String mp3FilePath, String wavFilePath) {
        try {
            AudioInputStream mp3Stream = getMP3Stream(mp3FilePath);
            AudioFormat mp3Format = mp3Stream.getFormat();
    
            AudioFormat pcmFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    mp3Format.getSampleRate(),
                    16,
                    mp3Format.getChannels(),
                    mp3Format.getChannels() * 2,
                    mp3Format.getSampleRate(),
                    false
            );
    
            AudioInputStream pcmStream = AudioSystem.getAudioInputStream(pcmFormat, mp3Stream);
            AudioSystem.write(pcmStream, AudioFileFormat.Type.WAVE, new java.io.File(wavFilePath));
    
            mp3Stream.close();
            pcmStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static AudioInputStream getMP3Stream(String mp3FilePath) throws JavaLayerException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(mp3FilePath);
            Bitstream bitstream = new Bitstream(fileInputStream);
            int channels = bitstream.readFrame().readStereo() ? 2 : 1;
            fileInputStream.close(); // Close and reopen to reset file pointer
    
            fileInputStream = new FileInputStream(mp3FilePath);
            return new AudioInputStream(new BufferedInputStream(fileInputStream),
                    new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                            AudioSystem.NOT_SPECIFIED,
                            16,
                            channels,
                            channels * 2,
                            AudioSystem.NOT_SPECIFIED,
                            false),
                    AudioSystem.NOT_SPECIFIED);
        } catch (Exception e) {
            throw new JavaLayerException("Error creating MP3 AudioInputStream", e);
        }
    }
    

    }

  5. fwx412269 commented on Dec 12, 2023

    @fwx412269
  6. reachvangala commented on Aug 1, 2025

    @reachvangala

    Hi,
    Please check the below code..

    import ws.schild.jave.*;

    import java.io.File;

    public class AudioConverter {
    public static void main(String[] args) {
    File source = new File("input.wav");
    File target = new File("output.mp3");

        // Set audio attributes for MP3
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(128000);
        audio.setChannels(2);
        audio.setSamplingRate(44100);
    
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
    
        // Encode
        Encoder encoder = new Encoder();
    
        try {
            encoder.encode(source, target, attrs);
            System.out.println("Conversion completed!");
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
    

    }

    check the maven dependancy..

    ws.schild jave-core 3.0.0
  7. fwx412269 commented on Aug 1, 2025

    @fwx412269
  8. huangfuyixiao commented on Aug 1, 2025

    @huangfuyixiao
  9. SiweiZhong commented on Aug 1, 2025

    @SiweiZhong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions