Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
If anyone is making a Java program or game that would benefit from playing OGG files on continuous loop, I fiddled with the source code for a while before finally creating something that worked!

NOTE: This code is used in my newest project, called Darkstone, for music playback. Darkstone can be found here: Darkstone Downloads

Code:
/*  Darkstone: A Map-Solving Game
Copyright (C) 2008-2010 Eric Ahnell

Any questions should be directed to the author via email at: products@puttysoftware.com
 */
package com.puttysoftware.music;

import java.io.IOException;
import java.net.URL;

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 Music {

	// Fields
	private URL url;
	private AudioInputStream stream;
	private AudioInputStream decodedStream;
	private AudioFormat format;
	private AudioFormat decodedFormat;
	private boolean stop;

	public Music(final URL loc) {
		this.url = loc;
		this.stop = false;
	}

	public void playLoop() {
		while (!this.stop) {
			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);
				}
			} catch (Exception e) {
				// Do nothing
			}
			SourceDataLine line = null;
			try {
				line = this.getLine(this.decodedFormat);
			} catch (LineUnavailableException lue) {
				// Do nothing
			}
			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();
					this.decodedStream.close();
					this.stream.close();
				} catch (IOException io) {
					// Do nothing
				}
			}
		}
	}

	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;
	}

	public void stopLoop() {
		this.stop = true;
	}
}

That code works, with 4 small libraries on the class path. They can be obtained here.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.