繁体   English   中英

如何播放给定绝对文件名的 mp3 文件?

[英]How to play an mp3 file given an absolute file name?

我在 Stack Overflow 上找到了一些可能的答案,但它们很旧,而且看起来它们使用了已弃用的技术。

我需要播放一个给定绝对文件名的 mp3 文件。

这是我尝试过的:

1.JavaFX

MediaPlayer player = new MediaPlayer(new Media(uriString)); 

我收到java.lang.IllegalStateException: Toolkit not initialized

我可能会找到一种初始化该工具包的方法,但我想知道这是否是首选方法。

2.Intellij UIUtil

final InputStream is = new FileInputStream(fileName);

UIUtil.playSoundFromStream(new Factory<InputStream>() {
    @Override
    public InputStream create() {
        return is;
    }
});

我得到Audio format is not yet supported: could not get audio input stream from input file

我做了更多的尝试,但这是我的记录。

到目前为止,唯一对我有用的是从 shell 播放文件:在 Mac 上,

Runtime.getRuntime().exec("afplay " + filePath);

但我更喜欢 Java 解决方案。 有任何想法吗?

  • 对于JavaFX

您可以在此处查看使用 javafx 播放 mp3 文件

  • 你来了,我最喜欢的部分:

您可以使用支持.mp3 的JLayer

例子

new Thread(()->{
   try {
        FileInputStream file = new FileInputStream("path ..../audio.mp3"); //initialize the FileInputStream
        Player player= new Player(file); //initialize the player
        player.play(); //start the player
    } catch (Exception e) {
       e.printStackTrace();
    }

 }).start();

笔记:

请注意,如果不是应用程序将堆叠,我正在使用单独的Thread原因。

通常来说,一般来说:

您必须使用外部库在Java播放 .mp3 之类的文件(尽管JavaFX支持 .mp3 但不是所有格式)

Java 仅支持 .wav

虽然这已经足够了。你只需要一个外部算法来播放其他音乐格式。所有其他格式最初来自 .wav,它们传递到一个算法中,然后它们变成.ogg,.mp3,.whatever

1.如之前提到的.mp3 JLayer.jar您可以将此jar 作为外部库导入到您的项目中。

2. JavaZoom也有和其他库支持.ogg,.speex,.flac,.mp3 ,按照上面的链接下载jlGui项目,你可以找到很多格式的库。

关于如何使用 java 播放 .wav 文件的stackoverflow 链接

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

}

暂无
暂无

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

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