Java 播放本地音频

来自姬鸿昌的知识库
跳到导航 跳到搜索

注意:只支持 wav 音频文件

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;

public class MusicPlayer {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {

        String pathname = "I:\\Program Files (x86)\\MediaEditor\\AudioFiles\\音效\\动物世界\\猫.wav";

        File f = new File(pathname);

        URI uri = f.toURI();

        AudioClip audioClip = Applet.newAudioClip(uri.toURL());

        audioClip.play();

        TimeUnit.SECONDS.sleep(5);

    }

}

示例2:同步播放

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AudioPlayer implements LineListener {
    /**
     * this flag indicates whether the playback completes or not.
     */
    boolean playCompleted;

    /**
     * Play a given audio file.
     *
     * @param audioFilePath Path of the audio file.
     */
    void play(String audioFilePath) {

        File audioFile = new File(audioFilePath);

        try {
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

            AudioFormat format = audioStream.getFormat();

            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip audioClip = (Clip) AudioSystem.getLine(info);

            audioClip.addLineListener(this);

            audioClip.open(audioStream);

            audioClip.start();

            while (!playCompleted) {
                // wait for the playback completes
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            audioClip.close();

        } catch (UnsupportedAudioFileException ex) {
            System.out.println("The specified audio file is not supported.");
            ex.printStackTrace();
        } catch (LineUnavailableException ex) {
            System.out.println("Audio line for playing back is unavailable.");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Error playing the audio file.");
            ex.printStackTrace();
        }

    }

    /**
     * Listens to the START and STOP events of the audio line.
     */
    @Override
    public void update(LineEvent event) {
        LineEvent.Type type = event.getType();

        if (type == LineEvent.Type.START) {
            System.out.println("Playback started.");

        } else if (type == LineEvent.Type.STOP) {
            playCompleted = true;
            System.out.println("Playback completed.");
        }

    }

}
public class AudioPlayerTest {

    public static void main(String[] args) {
        String pathname = "E:\\record\\2022\\11\\21\\%s.wav";
        for (int i = 1; i <= 10; i++) {
            new AudioPlayer().play(String.format(pathname, i + ""));
        }
    }
}

但是音频也有时长啊

考虑.png