Home >Java >javaTutorial >How to Play MP3s Using Java Sound API?
Playing an MP3 with Java Sound API
Java Sound API does not natively support MP3 files. To play MP3s, you can add the mp3plugin.jar to the runtime classpath. To do this:
Alternatively, you can use the BigClip class, a third-party library that supports arbitrary audio data lengths, including MP3s.
Code:
<code class="java">import javax.sound.sampled.*; import java.io.*; class AudioPlayer { public static void main(String[] args) throws Exception { // Custom BigClip class that supports MP3s BigClip clip = new BigClip(); // Open the MP3 file clip.open(AudioSystem.getAudioInputStream(new File("sound.mp3"))); // Start playing the sound clip.start(); } }</code>
The above is the detailed content of How to Play MP3s Using Java Sound API?. For more information, please follow other related articles on the PHP Chinese website!