Java tomcat声音

时间:2016-06-28 14:32:19

标签: java tomcat audio

我得到了下面的警告

  

2016年6月28日16:09:36.482警告[localhost-startStop-2]   org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads   Web应用程序[PickByVoice]似乎已启动一个帖子   命名为[Java Sound Event Dispatcher],但未能阻止它。这是   很可能会造成内存泄漏。堆栈跟踪线程:   java.lang.Object.wait(Native方法)   java.lang.Object.wait(Object.java:502)   com.sun.media.sound.EventDispatcher.dispatchEvents(EventDispatcher.java:182)   com.sun.media.sound.EventDispatcher.run(EventDispatcher.java:222)   java.lang.Thread.run(Thread.java:745)

我有一个web服务,当一个请求被调用时,webservice将播放一个声音文件。它不起作用。

它在eclipse中使用tomcat服务器。不知道为什么

希望有人可以帮助我

THX

1 个答案:

答案 0 :(得分:0)

这里有一些代码:

类Webservice

import java.io.IOException;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

//Import all the needed classfiles 
import org.PickByVoice.bluetooth.btautoconnect;
import org.PickByVoice.model.Languages;
import org.PickByVoice.model.WavFile;
import org.PickByVoice.service.AudioService;
import org.PickByVoice.service.UploadService;
import org.PickByVoice.exception.Statues;




@Path("restwebservice")                     //rootpath of the Servlet 
@Singleton                                  //Singleton is used to creat only one instance for all requests. So the Wav-Filelist needs to initiate only once.
@Produces(MediaType.TEXT_HTML) 
@Consumes(MediaType.TEXT_HTML)
public class RaspberryPiResources  {

    //creating instance, setting variables
    long startTime=System.currentTimeMillis();
    Languages listLanguages=new Languages();
    AudioService audioService = new AudioService();
    btautoconnect bluetoothAutoConnection=new btautoconnect();
    public static WavFile wavFileList;
    final String 
    String currentId="",currentShelfId="",currentstate="nothing scanned",language,location,currentthread;
    int i=0;

    public RaspberryPiResources(){
        language="eng";
        audioService.seterrorList("eng");   // Language specific error files are loaded
    }

    @GET
    @Path("init")
    public Response initialization(@DefaultValue("eng") @QueryParam ("language") String language,@QueryParam("warehouse") String country) throws InterruptedException, IOException{ 
        audioService.playinit(1L,language);         
        return Response.status(Statues.INITSUCESS).type(MediaType.TEXT_PLAIN).entity("ok").build();             //Status code 600
    }

@GET
    @Path("/sounds/messages/{messageId}")
    public Response outputErrorSounds(@PathParam("messageId") final long soundId) throws InterruptedException, IOException{
        currentthread=Thread.currentThread().getName();

        audioService.setcurrentThread(currentthread);
audioService.errorSound(soundId,language,currentstate,currentId,currentthread);                                             //Playing the error message and repeating the scann-Id
        return Response.status(Statues.OK).entity(Statues.OK.getReasonPhrase()).build();                                            //Status code 603
    }

类AudioService

    import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;




public class AudioService{

    public AudioInputStream audioInStream = null;
    public FilterIds filterId=new FilterIds();
    public File file;
    public SourceDataLine sourceDataLine;
    char firstcharacter;
    String compare="",currentThread="",Threadcompare="",vrrr="";
    WavFile wav;

    public AudioService() {
        // TODO Auto-generated constructor stub
    }

    //Stops and closes the SourceDataLine
    public void closeAudioLine(){
        sourceDataLine.stop();
        sourceDataLine.flush();
        sourceDataLine.close(); 
    }

    public void playinit(Long idOfSound, String language) throws InterruptedException, IOException{
        wav=new WavFile(language);
        String soundpath=wav.getErrorSoundFileList().get(idOfSound);
        currentThread="init";
        playAudio(soundpath);
    }


public synchronized String errorSound(Long soundId, String language,String currentState,String currentId, String Threadname) throws InterruptedException, IOException{
        currentThread="playerrorSound";
        if(!currentThread.equals("playerrorSound")){
            ack();
        }
        if(Threadname.equals(Thread.currentThread().getName()))
        {
        Threadcompare=Thread.currentThread().getName();
        String soundpath=wav.getErrorSoundFileList().get(soundId);                      
        playAudio(soundpath);                                                           
        try{    
            audioInStream.close();  
            }catch(IOException ioe)
            {ioe.printStackTrace();}

        }
        return "end";
        }