How can I play an MP3 file? [duplicate]

廉价感情. 提交于 2019-12-18 09:49:24

问题


I am trying to access an MP3 audio file saved in my source folder, so that I can play it in a Java program (application, not applet).

The problem I seem to be having is that I cannot import javax.media. I have been trying to install the Java media framework, but it gives me an error report:

"unable to execute...".

I use JDK v7 u45 and had tried it on JDK v7 u40, but I can't seem to figure out what to do. I tried calling

import javax.media.*;

and it returns a syntax error on the "media", claiming import is not available. Does jmf not work in Java 7?


回答1:


Try the following way. It's working and also import the following classes.

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

try{
    AudioInputStream audioInputStream =
        AudioSystem.getAudioInputStream(
            this.getClass().getResource("<Path of relative sound file in src folder>"));
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
}
catch(Exception ex)
{
}



回答2:


Your problem is likely that javax.media is not in your library. Make sure that you include the correct JAR file in your libraries (See Eclipse missing imports).


I previously did a bunch of searching to attempt to find out how to play audio in Java. What I found is that JavaFX is the easiest to use for playing MP3 files. For playing other inputs, I created a SourceDataLine (under the direction of this tutorial) and used that.


How to play MP3 music:

  • How do you play a long AudioClip?
  • Playing MP3 using Java Sound API (happens to be the basically the same answer as previous)
  • Playing .mp3 and .wav in Java? This answer suggests using JavaFX to play mp3's. It is the easiest method (as far as I know) for playing them. However, it has problems when used with a regular Swing application; this is addresed in the next answer.
  • Playing audio using JavaFX MediaPlayer in a normal Java application?



回答3:


The file MP3.java is a bare bones Java program that uses JLayer to play the MP3 file specified on the command line. It plays the MP3 file in a separate thread, so you are free to perform other computation while the song plays. In order to compile and execute it, you need to put the file jl1.0.jar in your current working directory (or classpath). Instructions for compiling and executing (depending on your operating system) are included in the .java file.




回答4:


For another method that I think is fairly simple and the most up-to-date method in playing MP3 files, use the JavaFX library. A very easy way to use JavaFX is to install the e(fx)clipse plugin. On that website, go to "install" at the top and follow the instruction on how to install the plugin into Eclipse. In Eclipse (after the plugin is installed), go to File -> New -> Other... -> JavaFX Project, and then your JavaFX imports will work.

The answer to the question Playing audio using JavaFX MediaPlayer in a normal Java application? (which was already linked to in another answer) was very helpful to me, and may also be helpful to you.



来源:https://stackoverflow.com/questions/19603450/how-can-i-play-an-mp3-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!