package com.jme3.audio.plugins;

import android.content.res.AssetFileDescriptor;
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.plugins.AndroidLocator;
import com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo;
import com.jme3.audio.AudioBuffer;
import com.jme3.audio.AudioKey;
import com.jme3.audio.SeekableStream;
import com.jme3.util.BufferUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class NativeVorbisLoader implements AssetLoader {
    
    private static class VorbisInputStream extends InputStream implements SeekableStream {

        private final NativeVorbisFile file;
        
        public VorbisInputStream(NativeVorbisFile file) {
            this.file = file;
        }
        
        @Override
        public int read() throws IOException {
            return 0;
        }
        
        @Override
        public int read(byte[] buf) throws IOException {
            return file.read(buf, 0, buf.length);
        }
        
        @Override
        public int read(byte[] buf, int off, int len) throws IOException {
            return file.read(buf, off, len);
        }
        
        @Override
        public long skip(long n) throws IOException {
            throw new IOException("Not supported for audio streams");
        }

        public void setTime(float time) {
            try {
                file.seekTime(time);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    
    private static AudioBuffer loadBuffer(AssetInfo assetInfo) throws IOException {
        AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;
        AssetFileDescriptor afd = null;
        NativeVorbisFile file = null;
        try {
            afd = aai.openFileDescriptor();
            int fd = afd.getParcelFileDescriptor().getFd();
            file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());
            ByteBuffer data = BufferUtils.createByteBuffer(file.totalBytes);
            file.readFully(data);
            AudioBuffer ab = new AudioBuffer();
            ab.setupFormat(file.channels, 16, file.sampleRate);
            ab.updateData(data);
            return ab;
        } finally {
            if (file != null) {
                file.close();
            }
            if (afd != null) {
                afd.close();
            }
        }
    }
    
    @Override
    public Object load(AssetInfo assetInfo) throws IOException {
        AudioKey key = (AudioKey) assetInfo.getKey();
        if (!(assetInfo instanceof AndroidLocator.AndroidAssetInfo)) {
            throw new UnsupportedOperationException("Cannot load audio files from classpath." + 
                                                    "Place your audio files in " +
                                                    "Android's assets directory");
        }
        
        if (key.isStream()) {
            throw new UnsupportedOperationException("Not supported yet. Come again.");
        } else {
            return loadBuffer(assetInfo);
        }
    }
}
@@ -318,16 +318,51 @@
 dioKey;%0A
+import com.jme3.audio.AudioStream;%0A
 import c
@@ -383,32 +383,32 @@
 SeekableStream;%0A
-
 import com.jme3.
@@ -664,16 +664,63 @@
 ream %7B%0A%0A
+        private final AssetFileDescriptor afd;%0A
         
@@ -802,32 +802,85 @@
 eam(
-NativeVorbisFile file) %7B
+AssetFileDescriptor afd, NativeVorbisFile file) %7B%0A            this.afd = afd;
 %0A   
@@ -1685,32 +1685,169 @@
     %7D%0A        %7D%0A
+        %0A        @Override%0A        public void close() throws IOException %7B%0A            file.close();%0A            afd.close();%0A        %7D%0A
     %7D%0A    %0A    p
@@ -2732,24 +2732,1042 @@
 %0A    %7D%0A    %0A
+    private static AudioStream loadStream(AssetInfo assetInfo) throws IOException %7B%0A        AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;%0A        AssetFileDescriptor afd = null;%0A        NativeVorbisFile file = null;%0A        boolean success = false;%0A        %0A        try %7B%0A            afd = aai.openFileDescriptor();%0A            int fd = afd.getParcelFileDescriptor().getFd();%0A            file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());%0A            %0A            AudioStream stream = new AudioStream();%0A            stream.setupFormat(file.channels, 16, file.sampleRate);%0A            stream.updateData(new VorbisInputStream(afd, file), file.duration);%0A            %0A            success = true;%0A            %0A            return stream;%0A        %7D finally %7B%0A            if (!success) %7B%0A                if (file != null) %7B%0A                    file.close();%0A                %7D%0A                if (afd != null) %7B%0A                    afd.close();%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A
     @Overrid
@@ -4286,80 +4286,35 @@
     
-throw new UnsupportedOperationException(%22Not supported yet. Come again.%22
+return loadStream(assetInfo
 );%0A 

Add streaming + looping support for Android vorbis audio