Playing .mp3 and .wav in Java?

前端 未结 14 2064
一整个雨季
一整个雨季 2020-11-22 06:52

How can I play an .mp3 and a .wav file in my Java application? I am using Swing. I tried looking on the internet, for something like this example:<

相关标签:
14条回答
  • 2020-11-22 07:28

    To give the readers another alternative, I am suggesting JACo MP3 Player library, a cross platform java mp3 player.

    Features:

    • very low CPU usage (~2%)
    • incredible small library (~90KB)
    • doesn't need JMF (Java Media Framework)
    • easy to integrate in any application
    • easy to integrate in any web page (as applet).

    For a complete list of its methods and attributes you can check its documentation here.

    Sample code:

    import jaco.mp3.player.MP3Player;
    import java.io.File;
    
    public class Example1 {
      public static void main(String[] args) {
        new MP3Player(new File("test.mp3")).play();
      }
    }
    

    For more details, I created a simple tutorial here that includes a downloadable sourcecode.

    0 讨论(0)
  • 2020-11-22 07:29

    I wrote a pure java mp3 player: mp3transform.

    0 讨论(0)
  • 2020-11-22 07:29

    Using standard javax.sound API, a single Maven dependency, completely Open Source (Java 7 or later required), this should be able to play most WAVs, OGG Vorbis and MP3 files:

    pom.xml:

     <!-- 
        We have to explicitly instruct Maven to use tritonus-share 0.3.7-2 
        and NOT 0.3.7-1, otherwise vorbisspi won't work.
       -->
    <dependency>
      <groupId>com.googlecode.soundlibs</groupId>
      <artifactId>tritonus-share</artifactId>
      <version>0.3.7-2</version>
    </dependency>
    <dependency>
      <groupId>com.googlecode.soundlibs</groupId>
      <artifactId>mp3spi</artifactId>
      <version>1.9.5-1</version>
    </dependency>
    <dependency>
      <groupId>com.googlecode.soundlibs</groupId>
      <artifactId>vorbisspi</artifactId>
      <version>1.0.3-1</version>
    </dependency>
    

    Code:

    import java.io.File;
    import java.io.IOException;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine.Info;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    import static javax.sound.sampled.AudioSystem.getAudioInputStream;
    import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
    
    public class AudioFilePlayer {
     
        public static void main(String[] args) {
            final AudioFilePlayer player = new AudioFilePlayer ();
            player.play("something.mp3");
            player.play("something.ogg");
        }
     
        public void play(String filePath) {
            final File file = new File(filePath);
     
            try (final AudioInputStream in = getAudioInputStream(file)) {
                 
                final AudioFormat outFormat = getOutFormat(in.getFormat());
                final Info info = new Info(SourceDataLine.class, outFormat);
     
                try (final SourceDataLine line =
                         (SourceDataLine) AudioSystem.getLine(info)) {
     
                    if (line != null) {
                        line.open(outFormat);
                        line.start();
                        stream(getAudioInputStream(outFormat, in), line);
                        line.drain();
                        line.stop();
                    }
                }
     
            } catch (UnsupportedAudioFileException 
                   | LineUnavailableException 
                   | IOException e) {
                throw new IllegalStateException(e);
            }
        }
     
        private AudioFormat getOutFormat(AudioFormat inFormat) {
            final int ch = inFormat.getChannels();
    
            final float rate = inFormat.getSampleRate();
            return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
        }
     
        private void stream(AudioInputStream in, SourceDataLine line) 
            throws IOException {
            final byte[] buffer = new byte[4096];
            for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
                line.write(buffer, 0, n);
            }
        }
    }
    

    References:

    • http://odoepner.wordpress.com/2013/07/19/play-mp3-using-javax-sound-sampled-api-and-mp3spi/
    0 讨论(0)
  • 2020-11-22 07:29

    Do a search of freshmeat.net for JAVE (stands for Java Audio Video Encoder) Library (link here). It's a library for these kinds of things. I don't know if Java has a native mp3 function.

    You will probably need to wrap the mp3 function and the wav function together, using inheritance and a simple wrapper function, if you want one method to run both types of files.

    0 讨论(0)
  • 2020-11-22 07:32

    Java FX has Media and MediaPlayer classes which will play mp3 files.

    Example code:

    String bip = "bip.mp3";
    Media hit = new Media(new File(bip).toURI().toString());
    MediaPlayer mediaPlayer = new MediaPlayer(hit);
    mediaPlayer.play();
    

    You will need the following import statements:

    import java.io.File;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    
    0 讨论(0)
  • 2020-11-22 07:32

    Use this library: import sun.audio.*;

    public void Sound(String Path){
        try{
            InputStream in = new FileInputStream(new File(Path));
            AudioStream audios = new AudioStream(in);
            AudioPlayer.player.start(audios);
        }
        catch(Exception e){}
    }
    
    0 讨论(0)
提交回复
热议问题