Note that there are some explanatory texts on larger screens.

plurals
  1. POJava thread ending with a delay
    primarykey
    data
    text
    <p>I am trying to play mp3's in my java application. The user has the ability to click a button and change the sound. </p> <p>The sound and switching works...however when a user tries to switch sounds, the new song begins playing over the old one for about a second, before the old one stops playing. I am trying use threads to handle everything. </p> <p>My MP3 class:</p> <pre><code>import java.applet.*; import javazoom.jl.player.Player; import java.io.*; import java.io.FileInputStream; import java.net.*; public class mp3 extends Thread implements Runnable{ private Player sound; private InputStream path; public mp3(String file_name) { path = mp3.class.getClassLoader().getResourceAsStream(file_name); } public void run(){ try { Player sound=new Player(path); sound.play(); } catch(Exception e) {System.out.println(e);} } } </code></pre> <p>And this is how I attempt to run the code in my main game. The MP3 object is called song. </p> <p>When the application initializes, the following code is run:</p> <pre><code>song = new mp3("default.mp3"); song.start(); </code></pre> <p>somebody tries to change it, the following code is run:</p> <pre><code>song.stop(); song = new mp3(t+".mp3"); song.start(); </code></pre> <p>Where t is a parameter of the file name. </p> <p>I'm pretty sure the problem relies in the run function of the MP3 object. I am completely new to threading so I would appreciate some help.</p> <p>Again, the delay is about 1 second from the time "stop" is called and the time it actually stops playing. So maybe throw a wait in there somewhere? </p> <p>Thanks!</p> <p><strong>EDIT:</strong> I am using code based off of here now:</p> <blockquote> <p><a href="http://introcs.cs.princeton.edu/java/faq/mp3/MP3.java.html" rel="nofollow">http://introcs.cs.princeton.edu/java/faq/mp3/MP3.java.html</a></p> </blockquote> <p>I have created a play and close function defined as follows:</p> <pre><code> public void close() { if (player != null) player.close(); } // play the MP3 file to the sound card public void play() { try { FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); player = new Player(bis); } catch (Exception e) { System.out.println("Problem playing file " + filename); System.out.println(e); } // run in new thread to play in background new Thread() { public void run() { try { player.play(); } catch (Exception e) { System.out.println(e); } } }.start(); </code></pre> <p>My problem is with the close function...the player object is always null and the close function does not execute...even though the play function initializes it. Why is it null?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload