Java 中的音乐循环 [英] Music Loop in Java

查看:23
本文介绍了Java 中的音乐循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我正在做一个游戏,我希望它有背景声音.我为它创建了一个类,并在我的 main.js 上调用它.我的代码如下:

I am doing a game and I want it to have a background sound. I created a class for it and I call it on my main. My code is as follows:

import sun.audio.*;
import java.io.*;

    public class Sound {

        public void music() {

            AudioStream backgroundMusic;
            AudioData musicData;
            AudioPlayer musicPlayer = AudioPlayer.player;
            ContinuousAudioDataStream loop = null;
            try {
                backgroundMusic = new AudioStream(new FileInputStream("chickendance.wav"));
                musicData = backgroundMusic.getData();
                loop = new ContinuousAudioDataStream(musicData);
                musicPlayer.start(loop);
            } catch (IOException error) { System.out.println(error);
            }
        }
    }

这是我的主要课程.

public class HangmanLauncher extends javax.swing.JFrame {

        public HangmanLauncher() {
            initComponents();
            Sound sound = new Sound();
            sound.music();
        }

我的问题是音乐无法播放.错误:java.io.IOException:无法从输入流创建音频流.这是什么意思?我的文件类型为 Microsoft Wave Sound Format,大小为 796kb.我可以知道我做错了什么吗?您的建议将不胜感激.提前致谢.

My problem is that the music doesn't play. Error: java.io.IOException: could not create audio stream from input stream. What does it mean? The type of my file is Microsoft Wave Sound Format and its size is 796kb. May I know what I am doing wrong? Your suggestions will be highly appreciated. Thank you in advance.

推荐答案

我的猜测是 wav 文件已经以 AudioStream 类没有的格式编码理解.我找不到该课程的文档 (??),但我会尝试另一个不是 Microsoft Wave Sound 的文件.同样,不知道该编码的细节,但它可能是 Microsoft 专有的,因此不在 AudioStream 的 Sun 实现中.

My guess is that the wav file has been encoded in a format the AudioStream class doesn't understand. I couldn't find the docs for the class (??) but I would try another file that isn't Microsoft Wave Sound. Again, don't know the specifics of that encoding but it being Microsoft it's probably proprietary and therefore not in the Sun implementation of the AudioStream.

这篇关于Java 中的音乐循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆