diff --git a/src/main/java/com/threerings/openal/FileStream.java b/src/main/java/com/threerings/openal/FileStream.java index 673c60f7..01ee304c 100644 --- a/src/main/java/com/threerings/openal/FileStream.java +++ b/src/main/java/com/threerings/openal/FileStream.java @@ -54,7 +54,7 @@ public class FileStream extends URLStream public void queueFile (File file, boolean loop) { try { - queueFile(file.toURI().toURL(), loop); + queueURL(file.toURI().toURL(), loop); } catch (MalformedURLException e) { log.warning("Invalid file url.", "file", file, e); } diff --git a/src/main/java/com/threerings/openal/ResourceStream.java b/src/main/java/com/threerings/openal/ResourceStream.java new file mode 100644 index 00000000..6e953521 --- /dev/null +++ b/src/main/java/com/threerings/openal/ResourceStream.java @@ -0,0 +1,87 @@ +// +// $Id$ +// +// Nenya library - tools for developing networked games +// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved +// http://code.google.com/p/nenya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.openal; + +import java.io.File; +import java.io.IOException; + +import java.net.MalformedURLException; +import java.net.URL; + +import static com.threerings.openal.Log.log; + +/** + * An audio stream read from one or more resources (via "resource://" URLs, so + * {@link com.threerings.resource.ResourceManager#activateResourceProtocol} must be called). + */ +public class ResourceStream extends URLStream +{ + /** + * Creates a new stream for the specified resource (from the default bundle). + * + * @param loop whether or not to play the file in a continuous loop + * if there's nothing on the queue + */ + public ResourceStream (SoundManager soundmgr, String resource, boolean loop) + throws IOException + { + this(soundmgr, "", resource, loop); + } + + /** + * Creates a new stream for the specified resource. + * + * @param loop whether or not to play the file in a continuous loop + * if there's nothing on the queue + */ + public ResourceStream (SoundManager soundmgr, String bundle, String resource, boolean loop) + throws IOException + { + super(soundmgr, new URL("resource://" + bundle + "/" + resource), loop); + } + + /** + * Adds a resource (from the default bundle) to the queue of files to play. + * + * @param loop if true, play this file in a loop if there's nothing else + * on the queue + */ + public void queueResource (String resource, boolean loop) + { + queueResource("", resource, loop); + } + + /** + * Adds a resource to the queue of files to play. + * + * @param loop if true, play this file in a loop if there's nothing else + * on the queue + */ + public void queueResource (String bundle, String resource, boolean loop) + { + try { + queueURL(new URL("resource://" + bundle + "/" + resource), loop); + } catch (MalformedURLException e) { + log.warning("Invalid resource url.", "resource", resource, e); + } + } +} diff --git a/src/main/java/com/threerings/openal/URLStream.java b/src/main/java/com/threerings/openal/URLStream.java index ced2026b..e5b310d2 100644 --- a/src/main/java/com/threerings/openal/URLStream.java +++ b/src/main/java/com/threerings/openal/URLStream.java @@ -46,7 +46,7 @@ public class URLStream extends Stream throws IOException { super(soundmgr); - _file = new QueuedFile(url, loop); + _url = new QueuedURL(url, loop); _decoder = StreamDecoder.createInstance(url); } @@ -56,9 +56,9 @@ public class URLStream extends Stream * @param loop if true, play this file in a loop if there's nothing else * on the queue */ - public void queueFile (URL url, boolean loop) + public void queueURL (URL url, boolean loop) { - _queue.add(new QueuedFile(url, loop)); + _queue.add(new QueuedURL(url, loop)); } @Override @@ -78,37 +78,37 @@ public class URLStream extends Stream throws IOException { int read = _decoder.read(buf); - while (buf.hasRemaining() && (!_queue.isEmpty() || _file.loop)) { + while (buf.hasRemaining() && (!_queue.isEmpty() || _url.loop)) { if (!_queue.isEmpty()) { - _file = _queue.remove(0); + _url = _queue.remove(0); } - _decoder = StreamDecoder.createInstance(_file.url); + _decoder = StreamDecoder.createInstance(_url.url); read = Math.max(0, read); read += _decoder.read(buf); } return read; } - /** The file currently being played. */ - protected QueuedFile _file; + /** The URL currently being played. */ + protected QueuedURL _url; - /** The stream decoder for the current file. */ + /** The stream decoder for the current URL. */ protected StreamDecoder _decoder; - /** The queue of files to play after the current one. */ - protected ArrayList _queue = Lists.newArrayList(); + /** The queue of URLs to play after the current one. */ + protected ArrayList _queue = Lists.newArrayList(); - /** A file queued for play. */ - protected class QueuedFile + /** A URL queued for play. */ + protected class QueuedURL { /** The URL of the file to play. */ public URL url; - /** Whether or not to play the file in a loop when there's nothing + /** Whether or not to play the URL in a loop when there's nothing * in the queue. */ public boolean loop; - public QueuedFile (URL url, boolean loop) + public QueuedURL (URL url, boolean loop) { this.url = url; this.loop = loop;