在 Java 程序中使用音乐

发布于 2024-09-10 16:12:23 字数 1363 浏览 6 评论 0原文

我正在尝试为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撞了怀 2024-09-17 16:12:23

您应该使用 JMF(Java 媒体框架)。为了您的兴趣:可以找到可接受的格式列表

简而言之,它支持 AIFF、AVI、GSM、MVR、MID、MPG、MP2、MOV、AU 和 WAV 文件。

但有一个解决方法,如所述 此处

附带说明一下,如果您添加
JMFRegistry 中的 mime 设置要映射
Windows Media 内容(例如 .asf
和 .wmv) 到内容类型
“video/mpeg”,JMF确实可以播放
Windows Media 或任何其他 DirectShow
文件(并且只有文件 - http 不起作用)。

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:

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).

与往事干杯 2024-09-17 16:12:23

如果 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.

请叫√我孤独 2024-09-17 16:12:23

刚刚也收到了这个

java.io.IOException: could not create AudioData object 

从来源 [1] 看来,这意味着“您的音频文件大小 > 1 MB”,但无论出于何种原因,它都不喜欢这样。也许是他们不适应的错误[?]。

如果您无论如何都希望循环适用于大文件,一种解决方法可能是按照建议使用 JMF。

[1] http://www.docjar.com/docs/ api/sun/audio/AudioStream.html#getData

Just got this, as well.

java.io.IOException: could not create AudioData object 

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文