简体   繁体   English

多线程只能在Java中播放背景音乐?

[英]Multithreading only way to play background music in Java?

I have a game with game logic happening in the main. 我有一个主要游戏逻辑发生的游戏。 I just added sound playing as per documentation I found: 我根据我发现的文档添加了声音播放:

//////////////////////SOUND/////////////////////////
     SourceDataLine soundLine = null;
     int BUFFER_SIZE = 64*1024;  // 64 KB

      // Set up an audio input stream piped from the sound file.
      try {
         File soundFile = new File("tim ph3 samplepart1.wav");
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
         AudioFormat audioFormat = audioInputStream.getFormat();
         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
         soundLine = (SourceDataLine) AudioSystem.getLine(info);
         soundLine.open(audioFormat);
         soundLine.start();
         int nBytesRead = 0;
         byte[] sampledData = new byte[BUFFER_SIZE];
         while (nBytesRead != -1) {
            nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
            if (nBytesRead >= 0) {
               // Writes audio data to the mixer via this source data line.
               soundLine.write(sampledData, 0, nBytesRead);
            }
         }
      } catch (UnsupportedAudioFileException ex) {
         ex.printStackTrace();
      } catch (IOException ex) {
         ex.printStackTrace();
      } catch (LineUnavailableException ex) {
         ex.printStackTrace();
      } finally {
         soundLine.drain();
         soundLine.close();
      }
     /////////////////////////////////////////////////////

It plays the file I specified out of the files in my project folder in Eclipse. 它播放我在Eclipse项目文件夹中的文件中指定的文件。

The problem? 问题? It blocks all game logic after it that appears in the main. 它会阻止主要出现的所有游戏逻辑。

This makes sense - the program is sequential and until the ENTIRE song is done...I figure the game can't go on. 这是有道理的 - 程序是顺序的,直到整个歌曲完成......我认为游戏不能继续下去。

This obviously isn't going to work, and it appears I'm going to have to go to dreaded multithreading...BUT BEFORE I DO...I wonder...is there a Java library or some other clever solution to avoid multithreading in this case? 这显然不会起作用,看起来我将不得不去做可怕的多线程...但是在我做之前......我想...是否有一个Java库或其他一些聪明的解决方案来避免在这种情况下多线程?

Yes, you need to use a separate thread. 是的,您需要使用单独的线程。 There's nothing to be afraid of. 没有什么可怕的。 Multithreading in Java is a piece of cake. Java中的多线程是一块蛋糕。 Look at the Concurrency packages. 查看并发包。

http://docs.oracle.com/javase/tutorial/essential/concurrency/ http://docs.oracle.com/javase/tutorial/essential/concurrency/

Beware: knowing how to start a thread and knowing how to safely multithread your programs are 2 different things. 注意:知道如何启动线程并知道如何安全地多线程化程序是两回事。 For now, make sure you avoid touching the same music from multiple threads. 现在,请确保避免从多个线程触摸相同的音乐。

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

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