[英]How to play an mp3 file in java
I am trying to play a song (mp3 file) in java. 我想在java中播放一首歌(mp3文件)。 I have been looking around for a few hours now and none of the ways I found worked properly.
我一直在寻找几个小时,我发现的方法都没有正常工作。
public void play()
{
String song = "song.mp3";
Media track = new Media(song);
MediaPlayer mediaPlayer = new MediaPlayer(track);
mediaPlayer.play();
}
I have tried doing that but it gives me errors. 我试过这样做,但它给了我错误。
I have imported JMF
and JLayer
. 我导入了
JMF
和JLayer
。
I have also read other questions that are like this one on this forum and none of them have helped me. 我也在这个论坛上读过其他类似的问题,但没有人帮助过我。
I just need a hand to help play an mp3 file. 我只需要一只手帮助播放一个mp3文件。
For this you'll need to install Java Media Framework (JMF) in your PC. 为此,您需要在PC中安装Java Media Framework(JMF) 。 One you have it installed,then try this piece of code:
一个你安装它,然后尝试这段代码:
import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
public static void main(String args[]) throws Exception
{
// Take the path of the audio file from command line
File f=new File("song.mp3");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}
You may run into unable to handle formaterror, that is because Java took out the MP3 support by default (pirate copyright issue), you are required to install a “JMF MP3 plugin” in order to play MP3 file. 您可能遇到无法处理formaterror,因为Java默认情况下取消了MP3支持(盗版版权问题),您需要安装“JMF MP3插件”才能播放MP3文件。
Go Java's JMF website to download it http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html 去Java的JMF网站下载它http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html
To be sure that you are using a supported format file, check here: 要确保您使用的是受支持的格式文件,请在此处查看:
http://www.oracle.com/technetwork/java/javase/formats-138492.html http://www.oracle.com/technetwork/java/javase/formats-138492.html
If you are using windows7, you may have to read this as well: 如果您使用的是Windows7,则可能还必须阅读:
https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45 https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45
The easiest way I found was to download the JLayer jar file from http://www.javazoom.net/javalayer/sources.html and to add it to the Jar library http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 我找到的最简单的方法是从http://www.javazoom.net/javalayer/sources.html下载JLayer jar文件并将其添加到Jar库http://www.wikihow.com/Add-JARs-对项目-建造-路径-在-Eclipse的%28Java 29%
Here is the code for the class 这是该类的代码
public class SimplePlayer {
public SimplePlayer(){
try{
FileInputStream fis = new FileInputStream("File location.");
Player playMP3 = new Player(fis);
playMP3.play();
}catch(Exception e){System.out.println(e);}
}
}
and here are the imports 这是进口
import javazoom.jl.player.*;
import java.io.FileInputStream;
How about JavaFX application- JavaFX应用程序如何 -
import java.net.URL;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class VLC extends Application {
void playMedia() {
String mp3 = "00- Tu Hi Mera.mp3";
URL resource = getClass().getResource(mp3);
System.out.println(resource.toString());
Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
}
public static void main(String args[]) {
new VLC().playMedia();
}
@Override
public void start(Stage stage) throws Exception {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.