Question

I try to make a class to play FLAC files. For this I use jFlac. So, for playing a song, I need to do:

Player p = new Player();
p.decode("PATH_TO_FLAC");

And I put that in the run() method of my class. If I start the thread, that's works. But I wonder how to make a pause. I have no control over the loop which is made in p.decode();, so I can't use wait and notify. But Thread.suspend() is deprecated. I don't know what to do.

My class:

public class FLACPlayer implements Runnable {
/**
 * Path of the song
 */
private String file;

private org.kc7bfi.jflac.apps.Player player;

/**
 * The thread launching the player
 */
private Thread playerThread;

/**
 * Constructor: initialize the player
 * 
 * @param str
 *            The path of the file to play
 */
public FLACPlayer(String str) {
    file = str;

    player = new org.kc7bfi.jflac.apps.Player();
    this.playerThread = new Thread(this, "AudioPlayerThread");
}

public void play() {
    playerThread.start();
}

@Override
public void run() {
    try {
        player.decode(file);
    } catch (IOException | LineUnavailableException e) {
    }
}

Thanks!

Was it helpful?

Solution 2

I think you have figure out your self how to implement this - But as Flavio suggested you can do the following

This method in PCMProcessors should be overridden -

  public void processPCM(ByteData pcm) {
    synchronized (pcmProcessors) {

        int suspensionValue=0;

        Iterator it = pcmProcessors.iterator();
        while (it.hasNext()) {
          //DEFINE THE FLAG FOR SUSPENSION HERE - Logic should be block set ???

           PCMProcessor processor = (PCMProcessor)it.next();
            processor.processPCM(pcm);
        }
    }
  }


    DEFINE A Variable : volatile int flag=Threshold.Value



    Poll this from the main thread which kicks DECODE method 

    Sleep and Resume for whatever time you want

OTHER TIPS

In general, threads should be stopped by setting some variable (using appropriate synchronization) that's periodically checked by the thread. The thread is then in charge of stopping itself. The same goes for suspension -- they can check a variable and then sleep for a time.

Suspending a thread where the code is not under your control is dangerous, because you don't know what it's doing. Maybe it grabbed a bunch of network ports (just for a moment, it thought) and now you've suspended it and those ports are mysteriously unavailable. Maybe it's doing some real-time stuff, and when you resume it will be in a totally broken state.

That said, you should read and understand why Thread.stop and friends are deprecated. Basically, it's not because they are themselves broken, but because they frequently lead to broken code. However, if you understand the risks and the code you're working with, there's nothing inherently wrong with calling them.

You can't pause a Java thread, and you don't want to, anyway.

The code you are using is just a simple example application, which does not contain any method to pause playing. You should check the source for the Player class; the code contained there is more complicated, but gives you more control over the playback process, doubtless including also the ability to pause.

you can solve the issue by interrupting a thread. when you interrupt a thread it shall throw a interrupted exception, catch the exception and make a wait lock on a object there. when you want to resume, just call notify on the object.

Object lock=new Object();

@Override
public void run() {
    try {
        player.decode(file);
    } catch (InterruptedException e) {

      synchronized(lock) 
      {
        lock.wait();
      }    

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top