Befehle an einen Thread senden?

Status
Nicht offen für weitere Antworten.

Blackskyliner

Mitglied
Hallo, ich hab ein Problem und zwar versuche ich mir einen kleinen Player zu Übungszwecken zu bauen.
Der SPielt auch schon ab alles kein Problem, nur komm ich nun zu dem Punkt, wo ein Pause, Resume, Stop auch schön wären...

Da hatte ich mir gedacht, da ich das Abspielen in einen Thread auslagere, dass ich einfach interrupt() aufrufe und der dann anhält das Lied abzuspielen... Machter aber nicht...
Weiß jemand eine Lösung für mein Problem?

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mediaplayer;


/**
 *
 * @author Blackskyliner
 */
public class MediaplayerControlClass {
    String file;
    Thread PlayerHandle;
    
    
    public MediaplayerControlClass(String file){
        this.setMedia(file);
    }
    
    public MediaplayerControlClass(){
        this.setMedia("");
    }
    
    public void setMedia(String file){
        this.file = file;
    }
    
    public void play(){
        PlayerHandle = new Thread(new MediaplayerClass("E:\\Musik\\HB-Popcorn.mp3"));
        PlayerHandle.start();
    }
    
    public void pause(){
        /*if(PlayerHandle != null){
            try {
                PlayerHandle.wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(MediaplayerControlClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }*/
        PlayerHandle.interrupt();
    }
    
    public void resume(){
        /*if(PlayerHandle != null){
            PlayerHandle.notify();
        }*/
        PlayerHandle.resume();
    }
    
    public void stop(){
        if(PlayerHandle != null){
            PlayerHandle.stop();
        }
    }
}

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mediaplayer;

import javax.sound.sampled.*;
import java.io.*;

/**
 *
 * @author Blackskyliner
 */
public class MediaplayerClass extends Thread {
    String file;
    boolean pause;
    
    @Override
    public void run(){
        testPlay();
    }
    
    public MediaplayerClass(String file){
        this.file = file;
    }
    
    public void play(){
        this.pause = false;
        this.testPlay();
    }
    
    public void pause(){
        this.pause = true;
    }
    
    
    public void testPlay()
    {
         String  filename = this.file;
        try {
            File file = new File(filename);
            AudioInputStream in= AudioSystem.getAudioInputStream(file);
            AudioInputStream din = null;
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(    AudioFormat.Encoding.PCM_SIGNED,
                                                            baseFormat.getSampleRate(),
                                                            16,
                                                            baseFormat.getChannels(),
                                                            baseFormat.getChannels() * 2,
                                                            baseFormat.getSampleRate(),
                                                            false
                                                        );
            din = AudioSystem.getAudioInputStream(decodedFormat, in);
            // Play now.
            rawplay(decodedFormat, din);
            in.close();
        } catch (Exception e) {
            //Handle exception.
        }
    } 
    
    private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException,                                                                                                LineUnavailableException
    {
        byte[] data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);
        if (line != null)
        {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1)
            {
                if(this.pause != true){
                    nBytesRead = din.read(data, 0, data.length);
                    if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
                }
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }
    }

    private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
    {
          SourceDataLine res = null;
          DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
          res = (SourceDataLine) AudioSystem.getLine(info);
          res.open(audioFormat);
          return res;
    } 
}

Code:
    private MediaplayerControlClass MediaplayerControlHandle;
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //testPlay("E:\\Musik\\HB-Popcorn.mp3");
        /*java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MediaplayerClass().testPlay("E:\\Musik\\HB-Popcorn.mp3");
            }
        });*/
        //MediaplayerClass t = new MediaplayerClass();
        //t.run("E:\\Musik\\HB-Popcorn.mp3");
        
        MediaplayerControlHandle = new MediaplayerControlClass("E:\\Musik\\HB-Popcorn.mp3");
        MediaplayerControlHandle.play();
        
        //new Thread(new MediaplayerClass("E:\\Musik\\HB-Popcorn.mp3")).start();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if(MediaplayerControlHandle != null){
            MediaplayerControlHandle.pause();
        }
    }

Ich kamm nähmlich echt nicht weiter, da ich nicht weiß, wie ich eine Interprocess Kommunikation zustande bringen kann...


Danke
Blackskyliner
 
Die interrupt()-Methode beendet nur sehr "spezielle" Operationen wie Thread.sleep() mit einer interruptedException, ansonsten setzt die Methode nur ein Flag und weiter nichts. Meine erste Idee bezüglich deines Problems wäre , im Abspiel-Thread periodisch auf isInterrupted() zu überprüfen und dann ggf. den Abspielvorgang zu stoppen o.ä. Aber da gibt es bestimmt Jemanden, der da ne bessere Idee hat.
 
Wenn ich isInterrupted() müsst ich das ja aber erstmal irgendwie schaffen den überhaupt zu interrupten...

Das ist ja das eigentliche und somit essenzielle Problem...

Unter C++ würd ich jetzt ne Globale Variable machen, da ein Mutex drauf haun und die dann immer dementsprechend überprüfen und lesend/schreibend darauf zugreifen...

Da man unter Java ja aber meines Erachtens nach keine Globalen Variablen unterhalb von Klass hat??? und es eine ziemlich bescheidene Variante wäre.... kommt des auch eigentlich nit in Frage...

Wäre halt schön, wenns jemand weiß 🙂

Ein Tutorial über Interprocess Kommunikation würde mir ja auch schon reichen (solange es Java spezifisch ist)


EDIT: suspend() und resume() machen das was ich gesucht habe...
Problem gelöst. Wenn jemanden trotzdem noch was einfällt, wie man z.B. über einen Htread Handle Funktionen der Thread-Abgeleiteten Klasse aufrufen kann der kann des Posten, da das auch noch interesannt wäre...

Soo häkchen ist gesetzt =)
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben