JavaでWavの部分再生簡単だなー。
テスト不十分だけど、ようは、byte skipすればいいみたい。
wavのミリ秒でのbyte量は
public static long countByteAt(AudioFormat format,long millisecond)
でいいと思う。
コード
/* * Created on 2004/10/03 * License Apache2.0 or Common Public License */ package org.jpn.xucker.commons.audio; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; /** * * */ public class JavaSoundAudioPlayer implements FileAudioPlayer,InputStreamAudioPlayer{ /* (non-Javadoc) * @see org.jpn.xucker.commons.audio.sp.FileAudioPlayer#play(java.io.File) */ public void play(File file) { try { // TODO Auto-generated method stub play(new FileInputStream(file)); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* (non-Javadoc) * @see org.jpn.xucker.commons.audio.sp.AudioInputStreamPlayer#play(javax.sound.sampled.AudioInputStream) */ public void play(InputStream stream) { try{ AudioInputStream inputStream=AudioSystem.getAudioInputStream(stream); //AudioInputStream inputStream = // AudioSystem.getAudioInputStream(audioFile); AudioFormat format =inputStream.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class,format); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(format); line.start(); int readBytes = 0; byte[] bytedata = new byte[1024]; while (readBytes != -1) { readBytes = inputStream.read(bytedata, 0, bytedata.length); if (readBytes >= 0) { //int writeBytes = line.write(bytedata, 0, readBytes); } } line.drain(); line.close(); inputStream.close(); }catch(Exception e){ e.printStackTrace(); } } public static long countByteAt(AudioFormat format,long millisecond){ if(millisecond==0){ return 0; } long rateIndex=(int)(format.getFrameRate()/1000*millisecond); return rateIndex*format.getSampleSizeInBits()/8*format.getChannels(); } public void play(InputStream stream,long startmillisecond,long endmillisecond) { try{ AudioInputStream inputStream=AudioSystem.getAudioInputStream(stream); //AudioInputStream inputStream = // AudioSystem.getAudioInputStream(audioFile); AudioFormat format =inputStream.getFormat(); long startByte=countByteAt(format,startmillisecond); long endByte=countByteAt(format,endmillisecond); long playByte=endByte-startByte; DataLine.Info info = new DataLine.Info(SourceDataLine.class,format); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(format); line.start(); //skip inputStream.skip(startByte); int readBytes = 0; byte[] bytedata = new byte[1024]; long readedByte=0; while (readBytes != -1) { readBytes = inputStream.read(bytedata, 0, bytedata.length); if (readBytes >= 0) { //int writeBytes = if(readedByte+readBytes<playByte){ line.write(bytedata, 0, readBytes); }else{ line.write(bytedata, 0, Math.min((int)(playByte-readedByte),readBytes)); break; } readedByte+=readBytes; } } //System.out.println(readedByte); line.drain(); line.close(); inputStream.close(); }catch(Exception e){ e.printStackTrace(); } } }