2015-05-09 167 views
1

我複製/粘貼代碼,本教程的OpenAL的聲音加載器:http://wiki.lwjgl.org/wiki/OpenAL_Tutorial_6_-_Advanced_Loading_and_Error_Handlesfor循環不斷gettging陷入了一個無限循環

但是,每當我嘗試加載多個聲,在getALBuffer方法循環陷入無限循環。我不認爲我改變了我複製/粘貼的任何代碼。調用i.next(),我是類型迭代器似乎總是返回true,但我不知道爲什麼。我會發布我的所有複製/粘貼代碼,以防萬一我意外地改變某些東西而不意識到它。代碼首先調用loadALData()。

public static String getALErrorString(int err) { 
     switch (err) { 
     case AL_NO_ERROR: 
      return "AL_NO_ERROR"; 
     case AL_INVALID_NAME: 
      return "AL_INVALID_NAME"; 
     case AL_INVALID_ENUM: 
      return "AL_INVALID_ENUM"; 
     case AL_INVALID_VALUE: 
      return "AL_INVALID_VALUE"; 
     case AL_INVALID_OPERATION: 
      return "AL_INVALID_OPERATION"; 
     case AL_OUT_OF_MEMORY: 
      return "AL_OUT_OF_MEMORY"; 
     default: 
      return "No such error code"; 
     } 
} 

public static String getALCErrorString(int err) { 
    switch (err) { 
    case ALC_NO_ERROR: 
     return "AL_NO_ERROR"; 
    case ALC_INVALID_DEVICE: 
     return "ALC_INVALID_DEVICE"; 
    case ALC_INVALID_CONTEXT: 
     return "ALC_INVALID_CONTEXT"; 
    case ALC_INVALID_ENUM: 
     return "ALC_INVALID_ENUM"; 
    case ALC_INVALID_VALUE: 
     return "ALC_INVALID_VALUE"; 
    case ALC_OUT_OF_MEMORY: 
     return "ALC_OUT_OF_MEMORY"; 
    default: 
     return "no such error code"; 
    } 
} 

public static int loadALBuffer(String path){ 

    int result; 
    IntBuffer buffer = BufferUtils.createIntBuffer(1); 

    // Load wav data into a buffers. 
    alGenBuffers(buffer); 

    if ((result = alGetError()) != AL_NO_ERROR){ 
     throw new OpenALException(getALErrorString(result)); 
    } 

    WaveData waveFile = LoadSound(path); 

    if (waveFile != null){ 
     alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate); 
     waveFile.dispose(); 
    }else{ 
     throw new RuntimeException("No such file: " + path); 
    } 

    // Do another error check and return. 
    if ((result = alGetError()) != AL_NO_ERROR) { 
     throw new OpenALException(getALErrorString(result)); 
    } 

    return buffer.get(0); 
} 

/** 
* 1) Checks if file has already been loaded. 
* 2) If it has been loaded already, return the buffer id. 
* 3) If it has not been loaded, load it and return buffer id. 
*/ 
@SuppressWarnings({ "unchecked", "rawtypes" }) 
public static int getLoadedALBuffer(String path) { 
    int count = 0; 
    for (Iterator i = loadedFiles.iterator(); i.hasNext(); count++) { 
     if (i.equals(path)) { 
      return ((Integer) buffers.get(count)).intValue(); 
     } 
     //I added this if statement to catch the infinite loop 
     if (count>=100000){ 
      System.out.println("getLoadedALBuffer was caught in an infinite loop!"); 
      return -1; 
     } 
    } 

    int buffer = loadALBuffer(path); 

    buffers.add(new Integer(buffer)); 
    loadedFiles.add(path); 

    return buffer; 
} 

/** 
* 1) Creates a source. 
* 2) Calls 'GetLoadedALBuffer' with 'path' and uses the returned buffer id as it's sources buffer. 
* 3) Returns the source id. 
*/ 
@SuppressWarnings("unchecked") 
public static int loadALSample(String path, boolean loop) { 
    IntBuffer source = BufferUtils.createIntBuffer(1); 
    int buffer; 
    int result; 

    // Get the files buffer id (load it if necessary). 
    buffer = getLoadedALBuffer(path); 

    //I added this to handle infinite loop error 
    if (buffer == -1){ 
     return -1; 
    } 

    // Generate a source. 
    alGenSources(source); 

    if ((result = alGetError()) != AL_NO_ERROR) 
     throw new OpenALException(getALErrorString(result)); 

    // Setup the source properties. 
    alSourcei(source.get(0), AL_BUFFER, buffer); 
    alSourcef(source.get(0), AL_PITCH, 1.0f); 
    alSourcef(source.get(0), AL_GAIN, 1.0f); 
    //alSource(source.get(0), AL_POSITION, sourcePos); 
    //alSource(source.get(0), AL_VELOCITY, sourceVel); 
    alSourcei(source.get(0), AL_LOOPING, (loop ? AL_TRUE : AL_FALSE)); 

    // Save the source id. 
    sources.add(new Integer(source.get(0))); 

    // Return the source id. 
    return source.get(0); 
} 

//I created this method 
public static int LoadWaveFile(String name, int location, boolean loop){ 
    return loadALSample(getWaveFilePath(name,location), loop); 
} 

/** 
* 1) Releases temporary loading phase data. 
*/ 
public static void killALLoadedData() { 
    loadedFiles.clear(); 
} 

// Source id's. 
public static int busterShot; 
public static int lifeGain; 

public static void loadALData() { 
    // Anything for your application here. No worrying about buffers. 
    busterShot = LoadWaveFile("MM_Shoot", SFX_FOLDER, false); 
    lifeGain = LoadWaveFile("MM_1up", SFX_FOLDER, false); 

    killALLoadedData(); 
} 

//I created these integers 
public static final int MUSIC_FOLDER = 0; 
public static final int SFX_FOLDER = 1; 
//I created this method 
public static WaveData LoadSound(String path){ 
    System.out.println("\nLoadSound() called!"); 
    WaveData sound = null; 
    try { 
     sound = WaveData.create(new BufferedInputStream(new FileInputStream(path))); 
     System.out.println(path+((sound!=null) ? " exists!" : " does not exist!")); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return sound; 
} 
//I created this method 
public static WaveData QuickLoad(String name, int location){ 
    WaveData sound = LoadSound(getWaveFilePath(name, location)); 
    return sound; 
} 
//I created this Method 
public static String getWaveFilePath(String name, int location){ 
    if (location == MUSIC_FOLDER){ 
     return "res/sounds/music/"+name+".wav"; 
    }else if(location == SFX_FOLDER){ 
     return "res/sounds/soundEffects/"+name+".wav"; 
    } 

    return null; 
} 

編輯:這裏是我的控制檯輸出,當我運行這段代碼:

LoadSound只有()呼籲!

res/sounds/soundEffects/MM_Shoot.wav exists!

getLoadedALBuffer陷入無限循環!

+0

你是否嘗試增加iterator而不是for循環中的count變量? – fingaz

+0

@芬戈茲哦,杜赫!哇,這是官方OpenAL網站自己的教程錯誤!謝謝你指出! – Entity1037

+0

只是gunna發佈作爲答案。 Upvote如果你可以! – fingaz

回答

2

遞增迭代器爲這樣:

for(Iterator i = ... ; i.hasNext(); i.next()) 
+0

你是不是指i.next()? –

+0

哎呀!這是我的C++出來。謝謝@AmirAfghani – fingaz

0

的解決方案是,即使迭代器的元件的數目與被添加更多的波數據文件增加,for循環永遠不會增加到迭代器的不同元件。

這是官方lwjgl網站自己的教程中的編程問題!我沒有想到,因此我的困惑。老實說,儘管我應該早點看到那個錯誤。我爲這個愚蠢的問題感到抱歉!