Java 使用JLayer播放MP3时指定开始/停止时间(毫秒)
我需要在java代码中播放MP3文件的一部分。我希望通过一个函数来实现这一点,该函数接受以毫秒为单位的启动和停止时间 JLayer包含一个名为AdvancedPlayer的类,该类有一个接受帧中开始和停止位置的方法:Java 使用JLayer播放MP3时指定开始/停止时间(毫秒),java,audio,mp3,jlayer,Java,Audio,Mp3,Jlayer,我需要在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
/**
* 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毫秒。然而,我需要一个更好的控制度,也就是说,我可能希望从4000毫秒到5000毫秒
我该怎么做?我需要先将MP3转换成.wav吗?如果26毫秒是MP3文件中可以达到的最佳分辨率,那么你就不走运了。将其转换为WAV可能可行,但源数据(即MP3)仍有基本分辨率限制
出于好奇,为什么要播放10毫秒的音频?如果26毫秒是MP3文件中可以达到的最佳分辨率,那么你就不走运了。将其转换为WAV可能可行,但源数据(即MP3)仍有基本分辨率限制
出于好奇,为什么要播放10毫秒的音频?我最后使用的解决方案是首先编写代码来播放wave文件的一部分(即从xxx ms到xxx ms),因为我还需要对该文件格式的支持。下面是代码:
File soundFile = new File(this.audioFilePath);
AudioInputStream originalAudioInputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat audioFormat = originalAudioInputStream.getFormat();
float startInBytes = (startTimeinMs / 1000 * audioFormat.getSampleRate() * audioFormat.getFrameSize());
float lengthInFrames = ((endTimeinMs - startTimeinMs) / 1000 * audioFormat.getSampleRate());
originalAudioInputStream.skip((long) startInBytes);
AudioInputStream partAudioInputStream = new AudioInputStream(originalAudioInputStream,
originalAudioInputStream.getFormat(), (long) lengthInFrames);
// code to actually play the audio input stream here
一旦这是工作,我写了这段代码转换成一个MP3临时波文件(我可以使用上面的代码)-这是使用JLayer和MP3SPI。我确实尝试过直接在转换后的音频流上执行上述操作,而不首先写入文件,但无法使其工作。我只使用可立即转换/写入的小MP3文件,所以我对这个解决方案很满意
File soundFile = new File(this.inputFilePath);
AudioInputStream mp3InputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat baseFormat = mp3InputStream.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream convertedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat, mp3InputStream);
File outputFile = new File(this.outputFilePath);
AudioSystem.write(convertedAudioInputStream, AudioFileFormat.Type.WAVE, outputFile);
我在最后使用的解决方案是首先编写代码来扮演wave文件的一部分(即从xxx ms到xxx ms),因为我还需要对该文件格式的支持。下面是代码:
File soundFile = new File(this.audioFilePath);
AudioInputStream originalAudioInputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat audioFormat = originalAudioInputStream.getFormat();
float startInBytes = (startTimeinMs / 1000 * audioFormat.getSampleRate() * audioFormat.getFrameSize());
float lengthInFrames = ((endTimeinMs - startTimeinMs) / 1000 * audioFormat.getSampleRate());
originalAudioInputStream.skip((long) startInBytes);
AudioInputStream partAudioInputStream = new AudioInputStream(originalAudioInputStream,
originalAudioInputStream.getFormat(), (long) lengthInFrames);
// code to actually play the audio input stream here
一旦这是工作,我写了这段代码转换成一个MP3临时波文件(我可以使用上面的代码)-这是使用JLayer和MP3SPI。我确实尝试过直接在转换后的音频流上执行上述操作,而不首先写入文件,但无法使其工作。我只使用可立即转换/写入的小MP3文件,所以我对这个解决方案很满意
File soundFile = new File(this.inputFilePath);
AudioInputStream mp3InputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat baseFormat = mp3InputStream.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream convertedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat, mp3InputStream);
File outputFile = new File(this.outputFilePath);
AudioSystem.write(convertedAudioInputStream, AudioFileFormat.Type.WAVE, outputFile);