无法播放音频文件

Can't get audio file to play

我似乎不明白为什么我的音频文件无法播放。音频文件是一个 wav 文件,只是。我收到的错误是 javax.sound.sampled.UnsupportedAudioFileException

public class MusicProgress {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame b = new JFrame();
    FileDialog fd = new FileDialog(b, "Pick a file: ", FileDialog.LOAD);
    fd.setVisible(true);
    final File file = new File(fd.getDirectory() + fd.getFile());
    //URI directory = new URI (fd.getDirectory() + fd.getFile());
    try {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
        AudioFormat audioFormat = inputStream.getFormat();
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.start();
    } catch (LineUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

我翻出了我的一些旧代码来播放音频文件。这可能有效:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

public class SoundPlayer {

Clip clip;

public SoundPlayer(String file){

    //if(clip.isRunning()){clip.stop();}
    try{
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(SoundPlayer.class.getResource(file));
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
    }catch(Exception err){err.printStackTrace(); JOptionPane.showMessageDialog(null, "SoundPlayer: "+err,null,0);}
}

}

并播放音频片段:

new SoundPlayer(filepath);

路径应如下所示:“/game/resources/sound/Explosion.wav” 正如您所说,它必须是一个 .wav 文件。

您必须使用外部库才能播放 .mp3 等文件

Java support only .wav

但那是 enough.All 你需要的是一个外部算法来播放其他音乐 formats.All 其他格式最初来自 .wav 他们传递给一个算法然后 boom 他们变成 .ogg,.mp3 ,.随便

1.A 非常令人印象深刻的库,支持 .mp3 JLayer.jar 您可以将此 jar 作为外部库导入到您的项目中。

2.If你再搜索一下你会发现JAudiotagger它很神奇,但很难使用。

3.Also 您可以使用 Java Media FrameWork,但它不支持太多格式。

4.JavaZoom 还有其他库支持 .ogg,.speex,.flac,.mp3

How to play .wav files with java

上的 Whosebug 链接

http://alvinalexander.com/java/java-audio-example-java-au-play-sound 不确定是否仍然适用于 java 8

这个:

   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.Clip;
   import javax.sound.sampled.DataLine;
   import javax.sound.sampled.LineEvent;
   import javax.sound.sampled.LineListener;
   import javax.sound.sampled.LineUnavailableException;
   import javax.sound.sampled.UnsupportedAudioFileException;


 public class AudioPlayerExample1 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() {
    File audioFile = new File("C:/Users/Alex.hp/Desktop/Musc/audio.wav");

    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(1000);
            } 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 static void main(String[] args) {
    AudioPlayerExample1 player = new AudioPlayerExample1();
    player.play();
} 

}

Java 不支持所有的 wav 格式。支持的最高质量是标准 CD 编码格式。有了这些文件,你应该没问题。该格式具有 16 位编码、44100 fps、little endian、立体声。您应该能够检查 wav 文件的属性并了解它是否匹配。

DAW 生成具有 24 位或 32 位编码,或 48000fps 或 92000fps 的 wav 文件越来越普遍。

可以使用 Audacity 等工具将它们转换为 "CD" 编码规范。