Home >Java >javaTutorial >How to Play MP3s Using Java Sound API?

How to Play MP3s Using Java Sound API?

DDD
DDDOriginal
2024-10-24 04:02:30569browse

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:

  1. Download mp3plugin.jar from the provided link or a trusted source.
  2. Add the JAR to your project's build path or classpath.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn