View Single Post
Old 21-10-2021, 13:10   #1
zabnicola
Member
 
Iscritto dal: Nov 2020
Messaggi: 70
[java] coda di tracce, la classe thread non esegue correttamente

Codice:
import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.LinkedList;
import java.util.Queue;

//il player cicla continuamente
//non fa niente (continue) se la coda è vuota
//non fa niente se è in corso una traccia
//preleva la traccia dalla coda, la esegue

public class Player extends Thread {
    private Configs conf;
    private Socket s;
    private Queue<String> q;
    private boolean isPlaying;

    public Player(Configs conf){
        this.conf = conf;
        this.isPlaying = false;
        this.q=new LinkedList<>();
    }

    public void run(){
        System.out.println("Player.run -");
        System.out.println("il player sta ciclando");
        System.out.println(" ");
        do {
            //System.out.println("size: " + this.q.size() + " isplaying: " + this.isPlaying);

            if (!this.q.isEmpty())
                System.out.print(this.q.size());

            if (this.q.size() > 0) {
                System.out.println("non vuota.");
                if (!this.isPlaying)
                    this.play(q.remove());
            }
        } while (this.q.size() <= 0);

        System.out.println("esce dal run???");
    }

    public void addToQueue(ArrayList<String> l){
        for(String e:l){
            if(!this.q.contains(e)){
                System.out.println("Player.addToQueue non contiene quindi aggiungo.");
                this.q.add(e);
                System.out.println("dopo aggiunto: " + this.q.size());
            }
            System.out.println("Player.addToQueue" + e);
        }
    }

    public void play(String name) {
        System.out.println("Player.play - name:" +name);
        try {
            File f;
            AudioFormat format;
            DataLine.Info info;
            Clip clip;

            f = new File(this.conf.getNameOutputDir()+name);

            AudioInputStream stream = AudioSystem.getAudioInputStream(f);
            format = stream.getFormat();
            info = new DataLine.Info(Clip.class, format);
            clip = (Clip) AudioSystem.getLine(info);

            CountDownLatch syncLatch = new CountDownLatch(1);

            // Listener which allow method return once sound is completed
            clip.addLineListener(e -> {

                if (e.getType() == LineEvent.Type.STOP) {
                    syncLatch.countDown();
                    isPlaying = false;
                }

                if (e.getType() == LineEvent.Type.START) {
                    isPlaying = true;
                }

            });

            clip.open(stream);
            clip.start();
            syncLatch.await();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Riporto qui il test
Codice:
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlayerTest {
    private Configs conf;
    private Player p;
    public PlayerTest(){
        String host = "127.0.0.1";
        int port = 5555;
        String inf="C:\\Users\\franc\\diskliketaxi\\1.b";
        String outf="C:\\Users\\franc\\diskliketaxi\\1.b.wav";
        long dimension=90000;
        String ind="C:\\Users\\franc\\diskliketaxi\\";
        String outd="C:\\Users\\franc\\diskliketaxi\\";
        long dchunk=100000;
        String npf="C:\\Users\\franc\\diskliketaxi\\1.b.wav";
        String sof="C:\\Users\\franc\\diskliketaxi\\prova.wav";

        conf=new Configs();
        conf.setHost(host);
        conf.setPort(port);
        conf.setNameInputFile(inf);
        conf.setNameOutputFile(outf);
        conf.setDimension(dimension);

        conf.setNameInputDir(outd);
        conf.setNameOutputDir(ind);
        conf.setDimensionChunk(dchunk);
        conf.setNamePlayFile(npf);
        conf.setNameSocketFile(sof);

        System.out.println("costruttore player");
        p=new Player(conf);
        p.start();

    }

    public Configs getConfig(){
        return this.conf;
    }

    public Player getPlayer(){
        return this.p;
    }

    public static void main(String[] args){
        PlayerTest pt=new PlayerTest();

        File directoryPath = new File(pt.getConfig().getNameInputDir());
        FilenameFilter bwavFilefilter = new FilenameFilter(){
            public boolean accept(File dir, String name) {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".b.wav")) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        String[] bwavFilesList = directoryPath.list(bwavFilefilter);

        ArrayList<String> l= new ArrayList<>(Arrays.asList(bwavFilesList));
        System.out.println("aggiungere...");
        pt.getPlayer().addToQueue(l);
    }

}
Ciclo infinitamente finchè la coda non ha un elemento, quando a un elemento faccio il play della traccia se questa non è in esecuzione.
l problema è
Codice:
pt.getPlayer().addToQueue(l);
che aggiunge la stringa nella coda, quindi dovrebbe entrare
Codice:
if (this.q.size() > 0) {
                System.out.println("non vuota.");
                if (!this.isPlaying)
                    this.play(q.remove());
            }
e invece vede la coda sempre vuota
perchè?

Ultima modifica di zabnicola : 21-10-2021 alle 13:12.
zabnicola è offline   Rispondi citando il messaggio o parte di esso