Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code to create mp3 convertor #30

Open
wendyjuly opened this issue Apr 17, 2017 · 5 comments
Open

code to create mp3 convertor #30

wendyjuly opened this issue Apr 17, 2017 · 5 comments

Comments

@wendyjuly
Copy link

No description provided.

@wendyjuly
Copy link
Author

Anyone?

@sanjeeth-07
Copy link

sanjeeth-07 commented Apr 17, 2017 via email

@fwx412269
Copy link

thanks for share.

@sanjeet28
Copy link

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

}

@fwx412269
Copy link

fwx412269 commented Dec 12, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants