【发布时间】:2025-12-03 23:35:01
【问题描述】:
我正在尝试使用
AudioInputStream跳过负数字节skip(long bytes)方法。
问题是试图(假设是少量字节......):
int skipped = audioInputStream.skip(-bytes);
总是返回 0 .... 我不知道该怎么办。
Here is the full code github 上的库。
我所做的是每次用户跳过音频时重新创建线路 当我当然可以做得更好时,这非常慢……只需向后或向前。现在它只支持转发...
/**
* Skip bytes in the File input stream. It will skip N frames matching to bytes, so it will never skip given bytes len
*
* @param bytes
* the bytes
* @return value bigger than 0 for File and value = 0 for URL and InputStream
* @throws StreamPlayerException
* the stream player exception
*/
public long seek(long bytes) throws StreamPlayerException {
long totalSkipped = 0;
//If it is File
if (dataSource instanceof File) {
//Check if the requested bytes are more than totalBytes of Audio
long bytesLength = getTotalBytes();
System.out.println("Bytes: " + bytes + " BytesLength: " + bytesLength);
if ( ( bytesLength <= 0 ) || ( bytes >= bytesLength )) {
generateEvent(Status.EOM, getEncodedStreamPosition(), null);
return totalSkipped;
}
logger.info(() -> "Bytes to skip : " + bytes);
Status previousStatus = status;
status = Status.SEEKING;
try {
synchronized (audioLock) {
generateEvent(Status.SEEKING, AudioSystem.NOT_SPECIFIED, null);
initAudioInputStream();
if (audioInputStream != null) {
long skipped;
// Loop until bytes are really skipped.
while (totalSkipped < ( bytes )) { //totalSkipped < (bytes-SKIP_INACCURACY_SIZE)))
//System.out.println("Running");
skipped = audioInputStream.skip(bytes - totalSkipped);
if (skipped == 0)
break;
totalSkipped += skipped;
logger.info("Skipped : " + totalSkipped + "/" + bytes);
if (totalSkipped == -1)
throw new StreamPlayerException(StreamPlayerException.PlayerException.SKIP_NOT_SUPPORTED);
logger.info("Skeeping:" + totalSkipped);
}
}
}
generateEvent(Status.SEEKED, getEncodedStreamPosition(), null);
status = Status.OPENED;
if (previousStatus == Status.PLAYING)
play();
else if (previousStatus == Status.PAUSED) {
play();
pause();
}
} catch (IOException ex) {
logger.log(Level.WARNING, ex.getMessage(), ex);
}
}
return totalSkipped;
}
这个问题的继续在这里...Java AudioInputStream how to support skip with negative number of bytes
【问题讨论】:
-
不是关于这个问题,但我真的很想知道那些不赞成一个问题,却让它成为最爱的人!
-
@snr Me :) 因为我喜欢哈哈
“Tell me and I forget, teach me and I may remember, involve me and I learn.” -
我从你那里得到了 NullPointerException :D
-
@snr “代码必须编译。输出必须是正确的。”如果你有这个问题的实现,你将永远受到祝福。
-
更改问题可能会使已为原始问题提供的任何答案无效。你应该问一个关于如何获得你想要的行为的新问题,并链接回这个问题作为参考。
标签: java audio skip audioinputstream