Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Threads, Audio Application, Audio loading twice error

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Threads, Audio Application, Audio loading twice error

    Just wondering if someone could she some light on this for me, i have an application for college which translates irish to english and vice versa, and the audio has to be done using threads. When i click play the audio plays, but when i change the word, and click play again, the audio plays both the previous audioclip and also the current simultaneously... Below is my code for the action and also the Audiopreloader class.. Thanks in advance

    PlayButton event:

    if (e.getSource() == playAudioBtn)
    {
    try {
    soundURL = new URL("file:" + System.getProperty("user.dir") + "/" + outputLabel.getText() + ".wav");

    }
    catch (MalformedURLException e1)
    {

    }
    System.out.println("Fetching Media "+ soundURL);
    new MediaPreLoader(soundURL);
    audioThread = new Thread() {
    public void run() {
    new MediaPreLoader(soundURL);
    MediaPreLoader.currentMedia.play();

    }
    };

    audioThread.start();
    }


    Media preLoader:


    import java.applet.*;
    import java.net.*;


    public class MediaPreLoader extends Thread {

    URL completeURL;
    static AudioClip currentMedia;

    public MediaPreLoader (URL mediaURL){

    completeURL = mediaURL;

    start();
    }
    public void run(){
    currentMedia = Applet.newAudioClip(completeURL);
    currentMedia.play();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Threads, Audio Application, Audio loading twice error

    One problem could be the use of static. Make the variables belong to an instance. Do NOT use static.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Threads, Audio Application, Audio loading twice error

    Wow, so easy when you know how, thanks again norm, your a lifesaver!!!

    Should be called super norm

Similar Threads

  1. audio
    By lanya in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 14th, 2013, 05:41 PM
  2. JMF Playing an audio error
    By vivekme77 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2013, 09:29 PM
  3. Audio In My Program
    By Fordy252 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 07:46 AM
  4. Audio In My Program
    By Fordy252 in forum Java Applets
    Replies: 0
    Last Post: January 29th, 2011, 01:27 PM
  5. sun.audio
    By bguy in forum Java SE APIs
    Replies: 3
    Last Post: November 12th, 2009, 12:17 AM

Tags for this Thread