0 Members and 1 Guest are viewing this topic.
Hey thanks for your help everybody, I was able to eventually get it to work, but unfortunately specific circumstances have prevented me from using the method I developed. It worked perfectly, it had volume, pan, and echo control, but it relied on input as a Wav file, and as I'm sure all of you know, wav files can be quite huge. greenfoot applets have a maximum size requirement, and so I was forced to switch to mp3 files, of which I have no control over whatsoever. So thanks for all your help, but unfortunately it looks like colume control will not play any part in Nightmare
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)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.FloatControl; import javax.sound.sampled.EnumControl; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import java.nio.ByteBuffer; import java.nio.ByteOrder;import javax.sound.sampled.Control;public class Sound extends Actor{ AePlayWave sound; private String filename; SourceDataLine auline; private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; int nBytesRead = 0; AudioInputStream audioInputStream; FloatControl volume; FloatControl pan; float vol = 0; int count = 0; boolean stopped = true; boolean paused = false; long prev = 0; public Sound(){ } public Sound(String wavfile){ filename = wavfile; File soundFile = new File(filename); if (!soundFile.exists()) { System.err.println("Wave file not found: " + filename); return; } audioInputStream = null; try { audioInputStream = AudioSystem.getAudioInputStream(soundFile); } catch (UnsupportedAudioFileException e1) { e1.printStackTrace(); return; } catch (IOException e1) { e1.printStackTrace(); return; } AudioFormat format = audioInputStream.getFormat(); auline = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { auline = (SourceDataLine) AudioSystem.getLine(info); auline.open(format); //auline.open(); } catch (LineUnavailableException e) { e.printStackTrace(); return; } catch (Exception e) { e.printStackTrace(); return; } if (auline.isControlSupported(FloatControl.Type.PAN)) { pan = (FloatControl) auline.getControl(FloatControl.Type.PAN); } if (auline.isControlSupported(FloatControl.Type.MASTER_GAIN)) { volume = (FloatControl) auline.getControl(FloatControl.Type.MASTER_GAIN); } } public void act(){ //must be called every frame of the game for sound to continually play if(stopped || paused){ prev = 0; return; } long time = System.currentTimeMillis(); long diff = 0; if(prev == 0){ diff = 5000; }else{ diff = time - prev; diff *= 200; } try { nBytesRead = audioInputStream.read(abData, 0, (int)diff); if (nBytesRead >= 0){ auline.write(abData, 0, nBytesRead); }else{ stopSound(); } } catch (IOException e) { e.printStackTrace(); return; } prev = time; } public void setVolume(float vol){ volume.setValue(vol); } public void setPan(float p){ pan.setValue(p); } public void stopSound(){ auline.stop(); auline.flush(); stopped = true; paused = false; } public void playSound(){ //try { // audioInputStream.reset(); //} catch (IOException e) { // e.printStackTrace(); // return; //} auline.start(); stopped = false; paused = false; } public void pauseSound(){ if(stopped || paused){ return; } auline.stop(); paused = true; } public void resumeSound(){ if(stopped || !paused){ return; } auline.start(); paused = false; } public void loopSound(){ }}