[英]Java Sound API unable to stop recording on a button click from UI of a web Application
我正在使用 Java Sound API 來創建 Web 應用程序。用戶界面(UI)有一個按鈕,點擊開始錄制語音,錄制的音頻保存在音頻文件(.wav ext)中。UI 還有一個播放按鈕訪問相同的音頻文件並播放錄音。 目前,我正在錄制指定持續時間的音頻,該持續時間在我的應用程序中進行硬編碼,或者在錄制開始時作為參數從 UI 傳遞。但現在的要求是在單擊 UI 上的按鈕時停止錄制。
現在的問題是在捕獲音頻時,我正在使用以下代碼打開目標行:
公共無效捕獲(int record_time){
try {
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if(!AudioSystem.isLineSupported(info))
{
System.out.println("Line not supported");}
final TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open();
targetDataLine.start();
Thread thread = new Thread()
{
@Override public void run()
{
AudioInputStream audioStream = new AudioInputStream(targetDataLine);
File audioFile = new File(fileName);
System.out.println("Recording is going to get saved in path :::: "+ audioFile.getAbsolutePath());
try{
AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);
}
catch (IOException e){
e.printStackTrace();
}
System.out.println("Stopped Recording");
}
};
thread.start();
Thread.sleep(record_time*1000);
targetDataLine.stop();
targetDataLine.close();
System.out.println("Ended recording test");
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
一旦記錄時間過去,上面的代碼將停止記錄。
但是我想要另一種方法,如下所示,當從 jsp 調用(停止按鈕單擊)時,它應該能夠獲得負責當前錄制並停止當前錄制的相同目標線對象。
下面的代碼對我不起作用,錄制繼續
public void stop(){
try{
System.out.println("stop :: in stop");
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if(!AudioSystem.isLineSupported(info))
{
System.out.println("Line not supported");}
final TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
System.out.println("stop Recording....");
System.out.println("targetDataLine.getLineInfo() in stop:::::::"+targetDataLine.getLineInfo());
System.out.println("targetDataLine.isActive() in stop:::::::"+targetDataLine.isActive());
System.out.println("targetDataLine.isOpen() in stop:::::::"+targetDataLine.isOpen());
targetDataLine.stop();
targetDataLine.close();
}
catch (Exception e) {
System.err.println("Line unavailable: " + e);
}
}
請提出一些解決方案。
另一件事可能..不確定但可能有一些作用是我從我的servlet調用這些方法的方式..下面是代碼....
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("buttonName");
String record_time = request.getParameter("time");
System.out.println("action:::"+action +"time in minutes:::"+ record_time);
int record_time_int =(Integer.parseInt(record_time));
String fileName = "studentXYZ.wav";
System.out.println("action:::"+action +"----wavfile:::"+fileName+"time in minutes:::"+ record_time);
Recorder01 record = new Recorder01();
if (action != null && (action.equalsIgnoreCase("Capture")) ){
record.capture(fileName, action,record_time_int);
request.setAttribute("fileName", fileName);
request.getRequestDispatcher("/recordAudio.jsp").forward(request, response);
}
else if (action != null && action.equalsIgnoreCase("play")) {
Recorder01 playsound = new Recorder01();
playsound.play(fileName);
System.out.println("finished playing recording");
request.setAttribute("fileName", fileName);
request.getRequestDispatcher("/recordAudio.jsp").forward(request, response);
}
else if(action!=null && action.equalsIgnoreCase("Stop")){
System.out.println("stop recording called");
record.stop();
request.getRequestDispatcher("/recordAudio.jsp").forward(request, response);
}
}
private TargetDataLine targetDataLine;
public void capture() {
if (targetDataLine == null) {
try {
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
}
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open();
targetDataLine.start();
Thread thread = new Thread() {
@Override
public void run() {
AudioInputStream audioStream = new AudioInputStream(targetDataLine);
File audioFile = new File(fileName);
System.out.println("Recording is going to get saved in path :::: " + audioFile.getAbsolutePath());
try {
AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Stopped Recording");
}
};
thread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-2);
}
}
}
public void stop() {
if (targetDataLine != null) {
System.out.println("stop :: in stop");
targetDataLine.stop();
targetDataLine.close();
targetDataLine = null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.