为此,您需要在PC中安装
Java Media Framework (JMF).一个你安装它,然后尝试这段代码:
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();
}
}
}
您可能遇到无法处理formaterror,因为Java默认情况下取消了MP3支持(盗版版权问题),您需要安装“JMF MP3插件”才能播放MP3文件.
要确保您使用的是受支持的格式文件,请在此处查看:
如果您使用的是Windows7,则可能还必须阅读: