gpt4 book ai didi

java.io.IOException : mark/reset not supported

转载 作者:行者123 更新时间:2023-11-30 02:52:03 24 4
gpt4 key购买 nike

我知道这个问题已经被问过很多次了,但在某些情况下情况有所不同,所以我无法弄清楚。当我在 Eclipse 中运行游戏时,一切都很顺利,游戏运行完美,但在导出后,它崩溃了。我可以打开游戏并在菜单中移动,但没有播放任何声音,并且在我点击播放后,游戏就卡住了,并且它用 cmd 给了我这个错误(我可以粘贴任何必要的类,但希望只有 Audio 类是必要的) :

java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at com.neet.Audio.JukeBox.load(JukeBox.java:26)
at com.neet.GameState.IntroState.<init>(IntroState.java:28)
at com.neet.GameState.GameStateManager.loadState(GameStateManager.java:48)
at com.neet.GameState.GameStateManager.setState(GameStateManager.java:72)
at com.neet.GameState.GameStateManager.<init>(GameStateManager.java:31)
at com.neet.Main.GamePanel.init(GamePanel.java:70)
at com.neet.Main.GamePanel.run(GamePanel.java:75)
at java.lang.Thread.run(Unknown Source)

这是音频类:`

package com.neet.Audio;

import java.util.HashMap;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class JukeBox {

private static HashMap<String, Clip> clips;
private static int gap;
private static boolean mute = false;

public static void init() {
clips = new HashMap<String, Clip>();
gap = 0;
}

public static void load(String s, String n) {
if(clips.get(n) != null) return;
Clip clip;
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(
JukeBox.class.getResourceAsStream(s)
);
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false
);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
clips.put(n, clip);
}
catch(Exception e) {
e.printStackTrace();
}
}

public static void play(String s) {
play(s, gap);
}

public static void play(String s, int i) {
if(mute) return;
Clip c = clips.get(s);
if(c == null) return;
if(c.isRunning()) c.stop();
c.setFramePosition(i);
while(!c.isRunning()) c.start();
}

public static void stop(String s) {
if(clips.get(s) == null) return;
if(clips.get(s).isRunning()) clips.get(s).stop();
}

public static void resume(String s) {
if(mute) return;
if(clips.get(s).isRunning()) return;
clips.get(s).start();
}

public static void loop(String s) {
loop(s, gap, gap, clips.get(s).getFrameLength() - 1);
}

public static void loop(String s, int frame) {
loop(s, frame, gap, clips.get(s).getFrameLength() - 1);
}

public static void loop(String s, int start, int end) {
loop(s, gap, start, end);
}

public static void loop(String s, int frame, int start, int end) {
stop(s);
if(mute) return;
clips.get(s).setLoopPoints(start, end);
clips.get(s).setFramePosition(frame);
clips.get(s).loop(Clip.LOOP_CONTINUOUSLY);
}

public static void setPosition(String s, int frame) {
clips.get(s).setFramePosition(frame);
}

public static int getFrames(String s) { return clips.get(s).getFrameLength(); }
public static int getPosition(String s) { return clips.get(s).getFramePosition(); }

public static void close(String s) {
stop(s);
clips.get(s).close();
}

}`

最佳答案

来自 Javadoc(我的亮点):

public static AudioInputStream getAudioInputStream(InputStream stream)
throws UnsupportedAudioFileException,
IOException

Obtains an audio input stream from the provided input stream. The stream must point to valid audio file data. The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException.

您似乎正在从包含代码和声音文件的 jar 文件运行。从 getResourceAsStream() 返回的 InputStream 将不起作用,因为它将由 ZipInputStream 支持(或实现),后者处理 mark () 作为无操作,并为 reset() 抛出 IOException

要解决该问题,您只需将输入流包装在 BufferedInputStream 中即可。

AudioInputStream ais =
AudioSystem.getAudioInputStream(
new BufferedInputStream(
JukeBox.class.getResourceAsStream(s)
)
};

关于java.io.IOException : mark/reset not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com