How to stop any audio track in JAVA

0

again bothered with my doubts, I have the following button that plays an audio file from a file in C:, but I need that with another button to be able to stop this audio track. How can I do it ?, in advance I appreciate any help suggestion or comment greetings.

  private void equipo7ActionPerformed(java.awt.event.ActionEvent evt) {
      String sonido1 = "C:/recursos/equipo10.wav";
        InputStream in = new FileInputStream(sonido1);
        AudioStream audio1 = new AudioStream(in);
        AudioPlayer.player.start(audio1);
        Arduino.sendData("H");
    } catch (Exception ex) {
        Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, 
 ex);
    }        // TODO add your handling code here:
}                              
    
asked by JESUS ESPINOSA 01.07.2017 в 17:31
source

1 answer

1

I share the solution in case someone serves XD, after googling and googling and suffering XD,

you just have to declare a variable within our class

    public AudioStream audio1;

on the button to start the music is as follows:

    private void equipo7ActionPerformed(java.awt.event.ActionEvent evt) {
    String sonido1 = "C:/recursos/equipo10.wav";
    InputStream in = new FileInputStream(sonido1);
     audio1 = new AudioStream(in);
    AudioPlayer.player.start(audio1);

  } catch (Exception ex) {
    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
  }      
   }       

And the button that stops the song uses the following code:

      private void altoActionPerformed(java.awt.event.ActionEvent evt) {        
       try {

        AudioPlayer.player.stop(audio1);
    } catch (Exception ex) {
        Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, 
  ex);
    }   
   }                       
    
answered by 01.07.2017 / 20:21
source