Javazoom tocar som

1 resposta
gqferreira

Boa noite pessoal. Depois de criar um "trocentos" exemplos consegui usar as bibliotecas da Javazoom para executar arquivos .ogg (padrão aberto de audio)
O problema é que preciso para um jogo, e, obviamente preciso executar vários arquivos de audio simultaneamente.

Minha classe utilitária para audio é essa:
package br.com.freeladev.utils;

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 Audio implements Runnable{
	private Sound s;
	public enum Sound{
		ROULLETS("br/com/freeladev/gui/sounds/roullets.ogg"),
		COINS("br/com/freeladev/gui/sounds/coins.ogg"),
		LEVER("br/com/freeladev/gui/sounds/lever.ogg"),
		LEVERHOVER("br/com/freeladev/gui/sounds/leverHover.ogg"),
		BUTTON("br/com/freeladev/gui/sounds/button.ogg"),
		ROULLETSINFINITY("br/com/freeladev/gui/sounds/roulletsInfinity.ogg");
		private String file;
		Sound(String file){
			this.file=file;
		}
		public String getFile(){
			return this.file;
		}
	}

	public void play(Sound s){
		this.s=s;
		new Thread(this).start();
	}
	@Override
	public void run() {
		try {
			File file = new File(getClass().getClassLoader().getResource(s.getFile()).toURI());
			soundPlay(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void soundPlay(File file)
	{
		try
		{			
			// Get AudioInputStream from given file.	
			AudioInputStream in= AudioSystem.getAudioInputStream(file);
			AudioInputStream din = null;
			if (in != null)
			{
				AudioFormat baseFormat = in.getFormat();
				AudioFormat  decodedFormat = new AudioFormat(
						AudioFormat.Encoding.PCM_SIGNED,
						baseFormat.getSampleRate(),
						16,
						baseFormat.getChannels(),
						baseFormat.getChannels() * 2,
						baseFormat.getSampleRate(),
						false);
				// Get AudioInputStream that will be decoded by underlying VorbisSPI
				din = AudioSystem.getAudioInputStream(decodedFormat, in);				
				// Play now !
				rawplay(decodedFormat, din);
				in.close();		
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void rawplay(AudioFormat targetFormat, 
			AudioInputStream din) throws IOException, LineUnavailableException
			{
		byte[] data = new byte[4096];
		SourceDataLine line = getLine(targetFormat);		
		if (line != null)
		{
			// Start
			line.start();
			int nBytesRead = 0, nBytesWritten = 0;
			while (nBytesRead != -1)
			{
				nBytesRead = din.read(data, 0, data.length);
				if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
			}
			// Stop
			line.drain();
			line.stop();
			line.close();
			din.close();
		}		
			}

	private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
	{
		SourceDataLine res = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
		res = (SourceDataLine) AudioSystem.getLine(info);
		res.open(audioFormat);
		return res;
	}
}

Essa classe é uma modificação do exemplo do javazoom.com (http://www.javazoom.net/vorbisspi/documents.html)

O problema é o seguinte, não consigo rodar dois arquivos simultaneamente, mesmo que através de instancias diferentes da classe Audio, diferentes arquivos de audio e/ou diferentes locais do sistema. Definitivamente não toca dois sons ao mesmo tempo. Parece que o "canal" fica bloqueado e não há mixagem de todos os sons pra sair por um canal só, mas não entendo isso e só arrisquei um palpite.

Algum gamer de plantão? (Tipo um Vinicius Godoy ^^)

1 Resposta

gqferreira

Alguém? :shock:

Criado 13 de junho de 2011
Ultima resposta 17 de jun. de 2011
Respostas 1
Participantes 1