简体   繁体   English

在Java游戏中播放音乐

[英]Playing music in a Java game

I'm trying to have some background music in a game I'm programming in Java. 我正在尝试用Java编程的游戏中加入一些背景音乐。 I have the coded, got no errors whatsoever. 我已经编码,没有任何错误。

The only problem is that no music plays. 唯一的问题是没有音乐播放。

Here's what I programmed: 这是我编写的:

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public void startBGMusic()

    try{

        InputStream in = new FileInputStream(new File("opening1.mid"));
        AudioStream audioStream = new AudioStream(in);
         AudioPlayer.player.start(audioStream);

    }catch(Exception error){
          System.out.println("File Not Found");
          System.out.println(error);
}

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

This works: 这有效:

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

public class AudioTest{

    private static AudioStream theStream;

    //  Main method
    public static void main(String[] args){

        try{
            theStream = new AudioStream(new FileInputStream("opening1.mid"));
            AudioPlayer.player.start(theStream);
        }
        catch(Exception e){e.printStackTrace();}
    }//end main

}//end AudioTest class

I would like to strongly recommend using the javax.sound.sampled library for game audio. 我强烈建议您将javax.sound.sampled库用于游戏音频。 As far as I know these old sun.audio libraries are obsolete or deprecated or something. 据我所知,这些旧的sun.audio库已过时或不赞成使用。 I don't use them at all, so I can't comment on how to work with them or whether code that works one one system will work on another. 我根本不使用它们,因此无法评论如何使用它们或在一个系统上工作的代码是否可以在另一个系统上工作。 I don't think you can safely assume that it will. 我认为您不能放心地相信它会。

The Java Tutorials Trail: Sound has what you need, although the tutorial is admittedly a difficult read. Java教程资源:声音是您所需要的,尽管该教程很难读懂。

The section on playing back sound will be the main point of focus. 播放声音的部分将成为重点。

Some folks make use of libraries, such as TinySound (available for free, on github), as a way to simplify game sf/x programming. 一些人利用诸如TinySound(在github上免费提供)之类的库,作为简化游戏sf / x编程的一种方式。 You can find out more about it and other sf/x libraries on the java game programming site java-gaming.org/ 您可以在Java游戏编程网站java-gaming.org/上找到有关它以及其他sf / x库的更多信息。

I find myself wondering why so many people end up trying to use these old sun libraries. 我发现自己想知道为什么这么多人最终尝试使用这些旧的Sun库。 Maybe it's just a side effect: the ones that do inevitably end up having problems and come asking questions on StackOverflow. 也许这只是副作用:那些不可避免的结果最终会遇到问题,并在StackOverflow上提出问题。 As a result, that population of is "over-represented"? 结果,该人口“被过度代表”了吗?

This method works perfectly for me, if it can help: 如果可以,此方法对我来说非常有效:

public synchronized void alarm(String audioString) 
{
    try
    {
        Clip crit = AudioSystem.getClip();
        AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(this.getClass().getResource(audioString));
        crit.open(inputStream1);
        crit.loop(Clip.LOOP_CONTINUOUSLY);
        crit.start();

    } catch (Exception e){System.err.println(e.getMessage());}
}

I found this code somewhere else on this website, just note it's not my idea, I'm not taking any credits for it. 我在该网站上的其他地方找到了此代码,只是请注意,这不是我的主意,我对此不抱任何赞赏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM