java 负数 字节,Java AudioInputStream如何支持负数字节跳过

我正在尝试使用AudioInputStream跳过负数的字节

skip(long bytes)方法.

问题是试图(假设有少量字节…):

int skipped = audioInputStream.skip(-bytes);

始终如回答Java AudioInputStream skip with negative number of bytes always returns 0所述返回0

我需要创建一个实现,它也支持负数的字节或类似向后的东西.

github上库的Here is the full code.

我要做的是每次用户跳过音频时都重新创建线路

当我当然可以做得更好的时候,这会非常慢… …向前还是向后.现在它仅支持转发…

/**

* 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值