在 Java 程序中使用音乐
我正在尝试为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:无法从输入流创建音频流
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
}
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 JMF(Java 媒体框架)。为了您的兴趣:可以找到可接受的格式列表
简而言之,它支持 AIFF、AVI、GSM、MVR、MID、MPG、MP2、MOV、AU 和 WAV 文件。
但有一个解决方法,如所述 此处:
You should use JMF (Java Media Framework). For your interest: The list of accepted formats can be found here.
In short, it supports AIFF, AVI, GSM, MVR, MID, MPG, MP2, MOV, AU and WAV files.
But there is a workarond as stated here:
如果 Java 可以处理 Windows Media 格式示例,我会感到惊讶 - 尝试将 .wmv 转换为 .wav 文件,然后看看它是否有效。
I would be surprised if Java can hand Windows Media format samples - try converting the .wmv to a .wav file and see if it works then.
刚刚也收到了这个
从来源 [1] 看来,这意味着“您的音频文件大小 > 1 MB”,但无论出于何种原因,它都不喜欢这样。也许是他们不适应的错误[?]。
如果您无论如何都希望循环适用于大文件,一种解决方法可能是按照建议使用 JMF。
[1] http://www.docjar.com/docs/ api/sun/audio/AudioStream.html#getData
Just got this, as well.
Appears from the source [1] that this means that "your audio file is size > 1 MB" and it doesn't like that for whatever reason. Maybe a bug [?] that they don't accomodate for this.
One work-around might be to use JMF instead, as suggested, if you want looping to work for large files anyway.
[1] http://www.docjar.com/docs/api/sun/audio/AudioStream.html#getData