Results 1 to 2 of 2
  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Question Can play audio, but can't stop it

    For the program I am currently working on, I need to be able to simply press my "Play" button to make audio play and my "Stop" button to make the audio stop playing. The play button works, but for whatever reason the stop button does not. I'm not really sure what's wrong.

    Here's the code for the play and stop buttons:

    Java Code:
       private void audBtnPlayActionPerformed(java.awt.event.ActionEvent evt) {
           toggleAudio("test.wav", true);
    
        private void audBtnStopActionPerformed(java.awt.event.ActionEvent evt) {
          toggleAudio("test.wav", false);
        }
        }
    And the code for toggleAudio:

    Java Code:
     public void toggleAudio(String filename, boolean play) {
            InputStream in;
            AudioStream as;
            try {
                in = new FileInputStream(filename);
                as = new AudioStream(in);
                if (play == true) {
                    AudioPlayer.player.start(as);
                } else {
                    AudioPlayer.player.stop(as);
    System.out.println("Why won't it stop????!");
                }
            } catch (IOException ex) {
                Logger.getLogger(Auditory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    I'll admit, I don't entirely know what I'm doing here - but It still seems like this should work. Help is much appreciated!

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Can play audio, but can't stop it

    Hi Lucien,

    The problem you have is both in and as are redeclared when the method is called. This means that the object to which the variables are pointing to is actually a new instance and not the same object as before.

    To correct this declare in and as as global variables and instanciate these upon initial execution.

    Regards.

Similar Threads

  1. J2ME, stop play after pressed button
    By derata in forum CLDC and MIDP
    Replies: 3
    Last Post: 04-20-2012, 12:00 PM
  2. how to stop audio(mp3)
    By shruts in forum New To Java
    Replies: 7
    Last Post: 08-07-2011, 04:19 PM
  3. Play Audio File
    By YapperNico in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 01:59 PM
  4. Play audio in the BlackBerry Browser
    By javierrebelde in forum Advanced Java
    Replies: 0
    Last Post: 04-30-2010, 07:51 PM
  5. How do i play an audio clip from a server?
    By JavaJenius in forum Networking
    Replies: 1
    Last Post: 03-08-2008, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •