3 Replies - 441 Views - Last Post: 28 March 2012 - 02:58 AM Rate Topic: -----

#1 Ultroman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-March 12

OggStreamer-Thread (to play music in-game) only works 1/3 of the times

Posted 27 March 2012 - 10:01 AM

I have a runnable Game.class and a runnable OggStreamer.class, to allow the music to run in its own separate thread, which I send as a parameter for the Game-class to use. When I run the game from my IDE the OggStreamer always works, but when I've exported it to a .jar-file, it only works 1/3 of the times I start it up. And it's not like the first piece of music doesn't start, and then the next piece of music started will play..it doesn't work at all, until I've started the game a few times.

Has any of you good people had a similar problem? I could understand it if it didn't work at all, which could indicate there was something wrong with the file-references to the music inside the jar-file...but it DOES work, just not consistently outside the IDE. I've seen similar posts around the web, but none have any definitive answers to them. I'm using vorbisspi 1.0.3, jorbis 0.0.15, tritonus_share and jogg 0.0.7 as referenced libraries for playing the ogg-files, if that helps.

NOTE: This is my first attempt at Game-programming, and I know it's not very pretty and that I am a sort of newbie :) There are many things I'd change about the general design, but I'm using this as a project to help me understand the problems I'll encounter when I sit down to design a real framework for my next game. I'm on the third semester of Computer Science, so I've coded quite a lot, just never any games. Mostly apps.

Start.class
    public static void main(String[] args) throws InterruptedException{
    ExecutorService threadExecutor = Executors.newCachedThreadPool();
    Game game = new Game(musicStreamer);
    game.init(); // loads stuff
    threadExecutor.execute( musicStreamer ); // start task1
    threadExecutor.execute( game ); // start task2
    threadExecutor.shutdown();
    }



OggStreamer.class
    public class OggStreamer implements Runnable{

	private URL url;
	private AudioInputStream stream;
	private AudioInputStream decodedStream;
	private AudioFormat format;
	private AudioFormat decodedFormat;
	private boolean stop, running;
	String filename = "";
	SourceDataLine line = null;

	public OggStreamer() {
		this.stop = true;
		this.running = true;
		this.url = null;
	}

	public void run() {
		while(running){
				while (!this.stop) {
					System.out.println("Playing Loop");
					try {
						// Get AudioInputStream from given file.
						this.stream = AudioSystem.getAudioInputStream(this.url);
						this.decodedStream = null;
						if (this.stream != null) {
							this.format = this.stream.getFormat();
							this.decodedFormat = new AudioFormat(
									AudioFormat.Encoding.PCM_SIGNED,
									this.format.getSampleRate(), 16,
									this.format.getChannels(),
									this.format.getChannels() * 2,
									this.format.getSampleRate(), false);
							// Get AudioInputStream that will be decoded by underlying
							// VorbisSPI
							this.decodedStream = AudioSystem.getAudioInputStream(
									this.decodedFormat, this.stream);
						}else{
							JOptionPane.showMessageDialog(null, "Stream = null!");
						}
					} catch (Exception e) {
						// Do nothing
						System.out.println("Could not get or decode audiostream");
					}
					line = null;
					try {
						line = this.getSourceDataLine(this.decodedFormat);
						FloatControl volume = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
						volume.setValue(1);
					} catch (LineUnavailableException lue) {
						// Do nothing
						JOptionPane.showMessageDialog(null, "Line is unavailable!");
					}
					if (line != null) {
						try {
							byte[] data = new byte[4096];
							// Start
							line.start();
							int nBytesRead = 0;
		
							while (nBytesRead != -1) {
								nBytesRead = this.decodedStream.read(data, 0,
										data.length);
								if (nBytesRead != -1) {
									line.write(data, 0, nBytesRead);
								}
								if (this.stop) {
									break;
								}
							}
		
							// Stop
							line.drain();
							line.stop();
							line.close();
						} catch (IOException io) {
							// Do nothing
							JOptionPane.showMessageDialog(null, "Line cannot start!");
						}
					}
				}
		}
	}

	private SourceDataLine getSourceDataLine(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;
	}


	public void startLoop(String filenameString) {
		this.filename = filenameString;
		System.out.println("Starting loop with: "+filenameString);
		this.url = this.getClass().getResource(filenameString);
		this.stop = false;
	}

	public void stopLoop() {
		System.out.println("Stopping loop");
		try {
			if(this.decodedStream!=null)this.decodedStream.close();
			if(this.stream!=null)this.stream.close();
		} catch (IOException e) {
		}
		this.stop = true;
		this.url = null;
	}
	
	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public URL getUrl() {
		return url;
	}

	public void setUrl(String string) {
		this.filename = string;
		this.url = this.getClass().getResource(string);
	}

	public String getFilename() {
		return filename;
	}
    }



Game.class uses the OggStreamer-class as such:
    if(musicStreamer!=null && !musicStreamer.getFilename().equals("/snm/sound/oggs/Prelude.ogg")){
	    		if(!musicStreamer.isStop())musicStreamer.stopLoop();
	    		musicStreamer.startLoop("/snm/sound/oggs/Prelude.ogg");
}



Is This A Good Question/Topic? 0
  • +

Replies To: OggStreamer-Thread (to play music in-game) only works 1/3 of the times

#2 Ultroman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-March 12

Re: OggStreamer-Thread (to play music in-game) only works 1/3 of the times

Posted 27 March 2012 - 10:07 AM

Sorry, there were some excerpts in the start.class. Here's the full one:

public class Start {
    public static void main(String[] args) throws InterruptedException{
    	OggStreamer musicStreamer = new OggStreamer();
    	ExecutorService threadExecutor = Executors.newCachedThreadPool();
    	
	Game game = new Game(musicStreamer);
        game.init();
        
        threadExecutor.execute( musicStreamer ); // start task1
        threadExecutor.execute( game ); // start task2
        
        threadExecutor.shutdown();
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: OggStreamer-Thread (to play music in-game) only works 1/3 of the times

Posted 27 March 2012 - 05:07 PM

There are a number of issues with your code, including the fact that you haven't provided specific details of the size and sample rate of the media that you are trying to play, as well as the specifics of the platform that you are running the release code on.
Was This Post Helpful? 0
  • +
  • -

#4 Ultroman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-March 12

Re: OggStreamer-Thread (to play music in-game) only works 1/3 of the times

Posted 28 March 2012 - 02:58 AM

View PostButchDean, on 27 March 2012 - 05:07 PM, said:

There are a number of issues with your code, including the fact that you haven't provided specific details of the size and sample rate of the media that you are trying to play, as well as the specifics of the platform that you are running the release code on.


Oh, I didn't realize the media-details would be an issue in this matter, since it always works while running from the IDE. The ogg-files are running a sample rate of 44100Hz, Stereo, and are between 1 and 15mb in filesize, encoded with variable bitrate 128-256 Kbps.

I'm running Windows 7 64-bit.
IBM Rational Software Architect (Eclipse IDE)

What are the remaining issues, if you please? And might you have any idea as to what can be causing this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1