Moved the code for registering extensions and creating instances to
StreamDecoder. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@154 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -21,32 +21,18 @@
|
||||
|
||||
package com.threerings.openal;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.lwjgl.openal.AL10;
|
||||
|
||||
/**
|
||||
* An audio stream read from one or more files.
|
||||
*/
|
||||
public class FileStream extends Stream
|
||||
{
|
||||
/**
|
||||
* Registers a class of {@link StreamDecoder} for the specified file extension.
|
||||
*/
|
||||
public static void registerExtension (String extension, Class clazz)
|
||||
{
|
||||
_extensions.put(extension, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new stream for the specified file.
|
||||
*
|
||||
@@ -58,7 +44,7 @@ public class FileStream extends Stream
|
||||
{
|
||||
super(soundmgr);
|
||||
_file = new QueuedFile(file, loop);
|
||||
_decoder = createDecoder(file);
|
||||
_decoder = StreamDecoder.createInstance(file);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,43 +79,13 @@ public class FileStream extends Stream
|
||||
if (!_queue.isEmpty()) {
|
||||
_file = _queue.remove(0);
|
||||
}
|
||||
_decoder = createDecoder(_file.file);
|
||||
_decoder = StreamDecoder.createInstance(_file.file);
|
||||
read = Math.max(0, read);
|
||||
read += _decoder.read(buf);
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and initializes a stream decoder for the specified file.
|
||||
*/
|
||||
protected StreamDecoder createDecoder (File file)
|
||||
throws IOException
|
||||
{
|
||||
String path = file.getPath();
|
||||
int idx = path.lastIndexOf('.');
|
||||
if (idx == -1) {
|
||||
Log.warning("Missing extension for file [file=" + path + "].");
|
||||
return null;
|
||||
}
|
||||
String extension = path.substring(idx+1);
|
||||
Class clazz = _extensions.get(extension);
|
||||
if (clazz == null) {
|
||||
Log.warning("No decoder registered for extension [extension=" + extension +
|
||||
", file=" + path + "].");
|
||||
return null;
|
||||
}
|
||||
StreamDecoder decoder;
|
||||
try {
|
||||
decoder = (StreamDecoder)clazz.newInstance();
|
||||
} catch (Exception e) {
|
||||
Log.warning("Error instantiating decoder [file=" + path + ", error=" + e + "].");
|
||||
return null;
|
||||
}
|
||||
decoder.init(new FileInputStream(file));
|
||||
return decoder;
|
||||
}
|
||||
|
||||
/** The file currently being played. */
|
||||
protected QueuedFile _file;
|
||||
|
||||
@@ -155,11 +111,4 @@ public class FileStream extends Stream
|
||||
this.loop = loop;
|
||||
}
|
||||
}
|
||||
|
||||
/** Registered decoder classes. */
|
||||
protected static HashMap<String, Class> _extensions = new HashMap<String, Class>();
|
||||
static {
|
||||
registerExtension("ogg", OggStreamDecoder.class);
|
||||
registerExtension("mp3", Mp3StreamDecoder.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user