Git Product home page Git Product logo

Comments (5)

sanjeeth-07 avatar sanjeeth-07 commented on May 22, 2024 1

from java.

sanjeet28 avatar sanjeet28 commented on May 22, 2024 1

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

}

from java.

wendyjuly avatar wendyjuly commented on May 22, 2024

Anyone?

from java.

fwx412269 avatar fwx412269 commented on May 22, 2024

thanks for share.

from java.

fwx412269 avatar fwx412269 commented on May 22, 2024

from java.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.