为此,您需要在 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();
   }
 }
}
您可能会遇到无法处理格式错误,这是因为Java默认取消了对MP3的支持(盗版问题),您需要安装“JMF MP3插件”才能播放MP3文件。
去 Java 的 JMF 网站下载它
http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html
为确保您使用的是受支持的格式文件,请在此处查看:
http://www.oracle.com/technetwork/java/javase/formats-138492.html
如果您使用的是 windows7,您可能还需要阅读以下内容:
https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45