在Java中单独的线程循环音频 [英] Looping audio on separate thread in Java

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

问题描述

我想提出一个2D RPG游戏,我很希望得到背景音乐的工作。我写了播放音乐上一个单独的线程声类,但我无法弄清楚如何使它循环。我的声音类是如下:

 包tileRPG.gfx;进口的java.io.File;
进口java.io.IOException异常;进口javax.sound.sampled.AudioFormat中;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
进口javax.sound.sampled.DataLine中;
进口javax.sound.sampled.LineUnavailableException;
进口javax.sound.sampled.SourceDataLine;
公共类Sound实现Runnable
{    私人字符串fileLocation =RES / bgMusic.wav    市民声音(){}    公共无效播放()
    {
        线程t =新主题(本);
        t.start();
    }    公共无效的run()
    {
        playSound(fileLocation);
    }    私人无效playSound(字符串文件名)
    {
        文件音效档=新的文件(文件名);
        的AudioInputStream的AudioInputStream = NULL;
        尝试
        {
            的AudioInputStream = AudioSystem.getAudioInputStream(音效档);
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
        AudioFormat的AudioFormat的= audioInputStream.getFormat();
        SourceDataLine的行= NULL;
        DataLine.Info信息=新DataLine.Info(SourceDataLine.class,AudioFormat的);
        尝试
        {
            行=(SourceDataLine的)AudioSystem.getLine(信息);
            line.open(AudioFormat的);
        }
        赶上(LineUnavailableException E)
        {
            e.printStackTrace();
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
        line.start();
        INT nBytesRead = 0;
        字节[] = abData新的字节[128000]。
        而(nBytesRead!= -1)
        {
            尝试
            {
                nBytesRead = audioInputStream.read(abData,0,abData.length);
            }
            赶上(IOException异常E)
            {
                e.printStackTrace();
            }
            如果(nBytesRead> = 0)
            {
                INT nBytesWritten = line.write(abData,0,nBytesRead);
            }
        }
        line.drain();
        line.close();
    }
}


解决方案

 公共无效的run()
{
    而(真)
    {
        playSound(fileLocation);
    }
}

这将创建一个无限循环。相应地调整。

当你启动一个线程,它会执行任何code那就是在的run()方法,然后退出。因此,这是循环会为了去反复播放声音(不阻止您的其他线程/ code)。

该解决方案还没有与当前的code停止该线程(因此停止播放)的方式,但我认为你是在阶段写的。当应用程序当前所做的线程将停止播放并退出。

I am making a 2d RPG game and i was wanting to get background music working. I wrote a sound class that plays the music on a separate thread but i cannot figure out how to make it loop. My Sound class is as follows:

package tileRPG.gfx;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;


public class Sound implements Runnable 
{

    private String fileLocation = "res/bgMusic.wav";

    public Sound() { }

    public void play() 
    {
        Thread t = new Thread(this);
        t.start();
    }

    public void run()
    {
        playSound(fileLocation);
    }

    private void playSound(String fileName) 
    {
        File soundFile = new File(fileName);
        AudioInputStream audioInputStream = null;
        try 
        {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        AudioFormat audioFormat = audioInputStream.getFormat();
        SourceDataLine line = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try 
        {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(audioFormat);
        } 
        catch (LineUnavailableException e) 
        {
            e.printStackTrace();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        line.start();
        int nBytesRead = 0;
        byte[] abData = new byte[128000];
        while (nBytesRead != -1) 
        {
            try 
            {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            if (nBytesRead >= 0) 
            {
                int nBytesWritten = line.write(abData, 0, nBytesRead);
            }
        }
        line.drain();
        line.close();
    }
}

解决方案

public void run ()
{
    while(true)
    {
        playSound(fileLocation);
    }
}

This creates an infinite loop. Adjust accordingly.

When you start a thread, it will execute any code that is in the run() method and then exit. So this is where the loop would go in order to repeatedly play the sound (without blocking your other threads/code).

This solution has no way of stopping this thread (and therefore stopping playback) with your current code, but I assume you are writing this in stages. The thread will stop playback and exit when the application does currently.

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

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