我正在尝试为java程序创建背景音乐的方法,但是当我单击播放按钮时,它显示IO异常错误。
package javaentertainment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.*;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Music
{
public static void main(String args[])
{
JFrame frame=new JFrame();
frame.setSize(100,100);
JButton button=new JButton("P L A Y");
frame.add(button);
button.addActionListener(new AL());
frame.show();
}
public static class AL implements ActionListener
{
public void actionPerformed(ActionEvent e) {
music();
}
}
public static void music()
{
AudioPlayer MGP=AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinousAudioDataStream loop=null;
try
{
BGM = new AudioStream(new FileInputStream("Vision.wmv"));
MD=BGM.getData();
loop=new ContinousAudioDataStream(MD);
}
catch (IOException ex)
{
System.out.println(ex);
}
MGP.start(loop); // word loop was underlined by netbeans
}
}
当我运行该程序并单击播放时,它显示以下错误, java.io.IOException:无法从输入流创建音频流
最佳答案
您应该使用 JMF(Java 媒体框架)。为了满足您的兴趣:可以找到可接受的格式列表 here .
简而言之,它支持 AIFF、AVI、GSM、MVR、MID、MPG、MP2、MOV、AU 和 WAV 文件。
但是有一个解决方法,如所述 here :
On a side note, if you add a mime-setting in JMFRegistry to map Windows Media content (such as .asf and .wmv) to the content-type "video/mpeg", JMF can actually play Windows Media or any other DirectShow file (and only file - http wont work).
关于java - 在 Java 程序中使用音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3274775/