Home > Java > javaTutorial > body text

How to Play MP3s Using Java Sound API?

DDD
Release: 2024-10-24 04:02:30
Original
497 people have browsed it

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>
Copy after login

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!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!