Java代码重叠而不是停止和启动的声音?

时间:2017-06-26 20:52:07

标签: java user-interface audio io awt

我们目前正在开发一个涉及创建一个在后台播放声音效果的Java应用程序的项目。截至目前,声音有自己的动作侦听器,功能齐全,可以区分不同的剪辑。我们面临的问题是当执行动作时,剪辑似乎彼此重叠而不是停止一个剪辑并开始下一个剪辑。我们认为问题出在stopClip()方法中,但不确定如何解决问题。运行时,捕获中断的异常,但剪辑不会停止播放。如果有帮助,可以更新actionListener!以下是播放和停止方法:

function getSelectedRowsAsJson()
{
    var ids = $('#yourTableId').bootgrid('getSelectedRows');  //Here, remember that getSelectedRows only returns the values of the selected data-identifier columns (meaning, only the values of the selected pkcolumn (in this example) will be returned). 

    var myDataArray = [];   //Create an array that will store the edited objects (key-value pairs)
    $(ids).each(function (index, data) {
        var editedData = {};    //Create an object to store the selected key-value pairs that have been edited.
        editedData.key = data;
        editedData.editedValue = $('#pk' + data).val(); //so here we retrieve the value of the input whose id equals "pkxxxx". In your example above, it could be "pk35630", and so on.
        myDataArray.push(editedData);
    });

    //If myDataArray has data then proceed. Otherwise, return
    if(myDataArray.length === 0)
        return;
    //At this point, myDataArray will look like this: [{key: 'pk35630', editedValue: 'blue bla blue'}, {key: 'pk35635', editedValue: 'bla bla bla'}]
    //Next, you can use myDataArray to post it through ajax and update your database accordingly, thereafter refresh the bootgrid.

    var postdata = JSON.stringify(myDataArray);
    $.ajax({
        url: your-url,
        data: postdata,
        type: 'post'
    }).done(function (data, textStatus, jqXHR) {
        //do your own stuff. for example, refresh/reload the bootgrid.
    });
}

然后是动作监听器

public void playMeow(File sound){
  try{
      meowClip = AudioSystem.getClip();
      meowClip.open(AudioSystem.getAudioInputStream(sound));
      meowClip.start();
  }catch(Exception e){}
}

public void stopClip(){
  try{
    if (meowClip.isRunning()){
      meowClip.stop();
    }
    throw new InterruptedException();
  }catch(InterruptedException e){
    meowClip.stop();
    System.out.println("Interrupted Exception");
  }catch(Exception ex){}
}

1 个答案:

答案 0 :(得分:0)

According to oracle documentation, getClip()不会返回当前播放的声音,而是可以用来播放音频文件的剪辑对象。

您需要传递对用于启动音频文件的剪辑的引用。这可以通过将其作为参数返回/传递或使用全局变量/ getter方法来完成。