Added a ResourceStream for convenience, changed "file" to "URL" in

various locations of URLStream.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1163 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Andrzej Kapolka
2011-05-05 01:40:21 +00:00
parent 4ee9d56fe7
commit e29bf08b7b
3 changed files with 103 additions and 16 deletions
@@ -54,7 +54,7 @@ public class FileStream extends URLStream
public void queueFile (File file, boolean loop) public void queueFile (File file, boolean loop)
{ {
try { try {
queueFile(file.toURI().toURL(), loop); queueURL(file.toURI().toURL(), loop);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
log.warning("Invalid file url.", "file", file, e); log.warning("Invalid file url.", "file", file, e);
} }
@@ -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);
}
}
}
@@ -46,7 +46,7 @@ public class URLStream extends Stream
throws IOException throws IOException
{ {
super(soundmgr); super(soundmgr);
_file = new QueuedFile(url, loop); _url = new QueuedURL(url, loop);
_decoder = StreamDecoder.createInstance(url); _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 * @param loop if true, play this file in a loop if there's nothing else
* on the queue * 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 @Override
@@ -78,37 +78,37 @@ public class URLStream extends Stream
throws IOException throws IOException
{ {
int read = _decoder.read(buf); int read = _decoder.read(buf);
while (buf.hasRemaining() && (!_queue.isEmpty() || _file.loop)) { while (buf.hasRemaining() && (!_queue.isEmpty() || _url.loop)) {
if (!_queue.isEmpty()) { 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 = Math.max(0, read);
read += _decoder.read(buf); read += _decoder.read(buf);
} }
return read; return read;
} }
/** The file currently being played. */ /** The URL currently being played. */
protected QueuedFile _file; protected QueuedURL _url;
/** The stream decoder for the current file. */ /** The stream decoder for the current URL. */
protected StreamDecoder _decoder; protected StreamDecoder _decoder;
/** The queue of files to play after the current one. */ /** The queue of URLs to play after the current one. */
protected ArrayList<QueuedFile> _queue = Lists.newArrayList(); protected ArrayList<QueuedURL> _queue = Lists.newArrayList();
/** A file queued for play. */ /** A URL queued for play. */
protected class QueuedFile protected class QueuedURL
{ {
/** The URL of the file to play. */ /** The URL of the file to play. */
public URL url; 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. */ * in the queue. */
public boolean loop; public boolean loop;
public QueuedFile (URL url, boolean loop) public QueuedURL (URL url, boolean loop)
{ {
this.url = url; this.url = url;
this.loop = loop; this.loop = loop;