简体   繁体   English

无法播放音频文件

[英]Can't get audio file to play

I can't seem to figure out why my audio file won't play. 我似乎无法弄清楚为什么我的音频文件无法播放。 The audio file is a wav file and is just. 音频文件是一个wav文件,只是。 The error i am getting is javax.sound.sampled.UnsupportedAudioFileException . 我得到的错误是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();
    }



}

}

I dug up some old code of mine to play an audio file. 我挖了一些我的旧代码来播放音频文件。 This might work: 这可能有效:

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);}
}

}

And to play an audio clip: 并播放音频剪辑:

new SoundPlayer(filepath);

The path should look somthing like this: "/game/resources/sound/Explosion.wav" And as you stated, it must be a .wav file. 路径应该看起来像这样:“/ game / resources /sound / Explosion.wav”正如你所说,它必须是.wav文件。

You have to use external libraries to play files like .mp3 您必须使用外部库来播放.mp3等文件

Java support only .wav Java仅支持.wav

but that's enough.All you need is an external algorithm to play other music formats.All the other format's came originally from .wav they pass into an algorithm and then boom they become .ogg,.mp3,.whatever 但这就足够了。你需要的是一个外部算法来播放其他音乐格式。所有其他格式来自.wav它们传递给算法然后繁荣它们变成.ogg,.mp3,.whatever

1.A very impressive library to use which support .mp3 JLayer.jar You can import this jar into your project as an external library. 1.一个非常令人印象深刻的库使用哪个支持.mp3 JLayer.jar您可以将此jar作为外部库导入到项目中。

2.If you search more you will find JAudiotagger it's just amazing but difficult to use. 2.如果你搜索更多,你会发现JAudiotagger它真的很棒但很难使用。

3.Also you can use Java Media FrameWork but whatever it doesnt support to much formats. 3.您也可以使用Java Media FrameWork,但它不支持多种格式。

4.JavaZoom has also and other libraries to support .ogg,.speex,.flac,.mp3 4.JavaZoom还有其他库来支持.ogg,.speex,.flac,.mp3

Links to stackoverflow on How to play .wav files with java 指向如何使用java播放.wav文件的 stackoverflow链接

And http://alvinalexander.com/java/java-audio-example-java-au-play-sound Not sure if that still works with java 8 并且http://alvinalexander.com/java/java-audio-example-java-au-play-sound不确定它是否仍适用于Java 8

This: 这个:

   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 does not support all wav formats. Java不支持所有wav格式。 The highest quality supported is a standard CD encoding format. 支持的最高质量是标准CD编码格式。 With those files, you should be okay. 有了这些文件,你应该没问题。 That format has 16-bit encoding, 44100 fps, little endian, stereo. 该格式具有16位编码,44100 fps,小端,立体声。 You should be able to inspect the properties of your wav file and learn if it matches or not. 您应该能够检查wav文件的属性并了解它是否匹配。

It is getting more and more common for DAWs to produce wav files that have 24-bit or 32-bit encoding, or 48000fps or 92000fps. DAW生成具有24位或32位编码或48000fps或92000fps的wav文件越来越常见。

It is possible to convert these to the "CD" encoding spec with a tool such as Audacity. 可以使用Audacity等工具将这些转换为“CD”编码规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM