使用 JLayer 播放 MP3 时指定开始/停止时间(以毫秒为单位)
我需要在我的java代码中播放MP3文件的一部分。 我希望通过一个接受以毫秒为单位的开始和停止时间的函数来做到这一点。
JLayer 包含一个名为 AdvancedPlayer 的类,它有一个接受帧中开始和停止位置的方法:
/**
* Plays a range of MPEG audio frames
* @param start The first frame to play
* @param end The last frame to play
* @return true if the last frame was played, or false if there are more frames.
*/
public boolean play(final int start, final int end) throws JavaLayerException
{
boolean ret = true;
int offset = start;
while (offset-- > 0 && ret) ret = skipFrame();
return play(end - start);
}
根据 这个,一帧持续26毫秒。 然而我需要比这更精细的控制,即我可能希望播放 40 毫秒到 50 毫秒。
我怎样才能做到这一点? 我需要先将 MP3 转换为 .wav 吗?
I need to play a part of an MP3 file in my java code. I wish to do this via a function which accepts the start and stop time in millisecs.
JLayer contains a class called AdvancedPlayer which has a method that accepts the start and stop position in frames:
/**
* Plays a range of MPEG audio frames
* @param start The first frame to play
* @param end The last frame to play
* @return true if the last frame was played, or false if there are more frames.
*/
public boolean play(final int start, final int end) throws JavaLayerException
{
boolean ret = true;
int offset = start;
while (offset-- > 0 && ret) ret = skipFrame();
return play(end - start);
}
According to this, a frame lasts 26millisecs. However I need a finer degree of control than this, i.e. I may wish to play from 40millisecs to 50millisecs.
How can I do this? Do I need to convert the MP3 to .wav first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终使用的解决方案是首先编写代码来播放波形文件的一部分(即从 xxx ms 到 xxx ms),因为我也需要对此文件格式的支持。 下面是代码:
一旦它工作起来,我编写了这段代码来将 MP3 转换为临时波形文件(然后我可以将其与上面的代码一起使用) - 这是使用 JLayer 和 MP3SPI。 我确实尝试简单地直接在转换后的音频流上执行上述操作,而不先写入文件,但无法使其工作。 我只使用可以立即转换/写出的小型 MP3 文件,因此我对这个解决方案很满意。
The solution I used in the end was to first write the code to play a part of a wave file (i.e. from xxx ms to xxx ms) as I also need support for this file format. Here's the code for that:
Once this was working I wrote this code to convert an MP3 to a temporary wave file (which I can then use with the above code) - this is using JLayer and MP3SPI. I did try simply performing the above directly on the converted audio stream without first writing out to a file but couldn't get it to work. I'm only using small MP3 files that convert/write out instantly so I'm happy with this solution.
如果 26 毫秒是您在 MP3 文件中可以达到的最高分辨率,那么您就不走运了。 将其转换为 WAV 可能可行,但源数据(即 MP3)仍然具有基本的分辨率限制。
出于好奇,为什么要播放 10 毫秒的音频?
If 26 milliseconds is the finest resolution you can achieve in an MP3 file, then you're out of luck. Converting it to WAV might work, but the source data (i.e. the MP3) stil has that basic resolution limit.
Out of curiosity, why do you want to play 10 milliseconds worth of audio?