在Java程序中使用的音乐 [英] Using music in a java program
问题描述
我在尝试创建一个Java程序中的背景音乐的方法,但它显示一个IO excedption错误,当我点击播放按钮。
I was trying out the method of creating a background music for a java program, but it displayed an IO excedption error when i clicked the play button.
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异常:无法从输入流创建音频流
When I run the program and click on play it displays the following error, java.io.IOException: could not create audio stream from input stream
推荐答案
您应该使用JMF(Java媒体框架)。为了您的利益:所接受的格式列表可以发现的这里。
You should use JMF (Java Media Framework). For your interest: The list of accepted formats can be found here.
总之,它支持AIFF,AVI,GSM,MVR,MID,MPG,MP2,MOV,AU和WAV文件。
In short, it supports AIFF, AVI, GSM, MVR, MID, MPG, MP2, MOV, AU and WAV files.
但是,作为规定的这里:
在一个侧面说明,如果添加
哑剧设定在JMFRegistry映射
Windows媒体内容(如的.asf
和.WMV)的内容类型
视频/ MPEG,JMF能真正发挥
Windows媒体或任何其他的DirectShow
文件(只有文件 - HTTP无法工作)。
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程序中使用的音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!