SoundEncodMP3.class : OSの録音デバイスから10秒間録音し test.mp3 に保存する。
■必要なファイル
●tritonus_mp3.jar
●tritonus_share.jar
http://www.tritonus.org/plugins.html
zipファイルのままでもかまわない
●lametritonus.dll
http://www.tritonus.org/plugins.html
tritonus-mp3enc-2001-04-25.zip に含まれる
●lame_enc.dll (LAME 3.96.1 2004-09-14)
http://www.rarewares.org/mp3.html
lame3.96.1.zip に含まれる
■動作確認環境
OS:Windows XP pro SP2、Windows 2000 pro SP6
JRE:1.4.2_05
コンパイル:javac -classpath .;tritonus_mp3.zip;tritonus_share.zip SoundEncodMP3.java
実行:java -cp .;tritonus_mp3.zip;tritonus_share.zip SoundEncodMP3
■ファイル名 : SoundEncodMP3.java
public class SoundEncodMP3 extends Thread
{
private javax.sound.sampled.AudioInputStream audioInputStream;
private javax.sound.sampled.AudioFileFormat.Type type;
private java.io.File file;
private SoundEncodMP3(
javax.sound.sampled.AudioInputStream audioInputStream,
javax.sound.sampled.AudioFileFormat.Type type,
java.io.File file )
{
this.audioInputStream = audioInputStream;
this.type = type;
this.file = file;
}
public void run()
{
try{
javax.sound.sampled.AudioSystem.write( audioInputStream, type, file );
}catch( Exception exception ){
exception.printStackTrace( System.err );
}
}
public static void main( String[] args )
{
final javax.sound.sampled.AudioFormat.Encoding AudioFormat_Encoding_MPEG1L3 =
org.tritonus.share.sampled.Encodings.getEncoding( "MPEG1L3" );
final javax.sound.sampled.AudioFileFormat.Type AudioFileFormat_Type_MP3 =
org.tritonus.share.sampled.AudioFileTypes.getType( "MP3", "mp3" );
javax.sound.sampled.TargetDataLine targetDataLine = null;
try{
javax.sound.sampled.AudioFormat audioFormat =
new javax.sound.sampled.AudioFormat(
48000.0F, 16, 2, true, true );
targetDataLine =
(javax.sound.sampled.TargetDataLine)javax.sound.sampled.AudioSystem.getLine(
new javax.sound.sampled.DataLine.Info(
javax.sound.sampled.TargetDataLine.class,
audioFormat )
);
targetDataLine.open();
targetDataLine.start();
javax.sound.sampled.AudioInputStream audioInputStreamPlay =
new javax.sound.sampled.AudioInputStream( targetDataLine );
javax.sound.sampled.AudioInputStream audioInputStreamRec =
javax.sound.sampled.AudioSystem.getAudioInputStream(
AudioFormat_Encoding_MPEG1L3,
audioInputStreamPlay );
SoundEncodMP3 recorder = new SoundEncodMP3(
audioInputStreamRec,
AudioFileFormat_Type_MP3,
new java.io.File( "test.mp3" ) );
recorder.start();
Thread.sleep( 10000 );
targetDataLine.stop();
recorder.join();
}catch( Exception exception ){
exception.printStackTrace( System.err );
System.exit( 1 );
}finally{
if( null != targetDataLine )
targetDataLine.close();
System.exit( 0 );
}
}
}