2009-06-30 4 views
1

Java 코드에서 MP3 파일의 일부를 재생해야합니다. 이 작업은 밀리 초 단위로 시작 및 종료 시간을 허용하는 함수를 통해 수행합니다.JLayer를 사용하여 MP3를 재생할 때 시작/정지 시간 지정 (밀리 세컨드)

/** 
* 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); 
} 

this에 따르면, 프레임은 지속 26millisecs :

JLayer는 시작을 허용하고 프레임 위치를 중지하는 방법이있다 AdvancedPlayer 불리는 클래스를 포함한다. 그러나 이보다 정밀한 통제가 필요합니다. 즉, 40 밀리 초에서 50 밀리까지 플레이하고 싶을 수 있습니다.

어떻게하면됩니까? 먼저 MP3를 .wav로 변환해야합니까?

답변

1

나는 결국이 파일 형식에 대한 지원이 필요하므로 웨이브 파일의 일부 (즉, xxx ms에서 xxx ms까지)를 재생하는 코드를 작성했습니다. -이 JLayer 및 MP3SPI을 사용하고이되면

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 

나는 (I 다음 위의 코드와 함께 사용할 수있는) 임시 웨이브 파일에 MP3를 변환하는 코드를 작성 작업을했다 : 여기에 대한 코드입니다. 먼저 파일을 쓰지 않고 변환 된 오디오 스트림에서 직접 위의 작업을 수행하려고했지만 작동시키지 못했습니다. 나는 변환/즉시 작성하는 작은 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); 
0

26 밀리 초가 MP3 파일에서 얻을 수있는 최상의 해상도라면, 운이 좋지 않습니다. WAV로 변환해도 작동하지만 원본 데이터 (즉, MP3)는 기본 해상도 제한이 있습니다.

호기심에서 왜 10 밀리 초 분량의 오디오를 듣고 싶습니까?