musicplayer spielt mit threads nicht mehr ab

Status
Nicht offen für weitere Antworten.

der_claude

Neues Mitglied
Hallo miteinander

Bin hier ganz neu und habe ein Problem, und hab zwar ähnliche Threads gefunden, aber keinen, der mir wirklich weiterhilft

Ich will mir zur Zeit einen einfachen Musik-Player programmieren

Folgender Code bisher:

Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.JList.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.midi.*;

import javax.swing.JButton;
import javax.swing.JFrame;



/** @author Claudius
 * Music-Player fuer Informatik II
 */
public class MusicPlayer extends JFrame implements ActionListener{
    JButton play = new JButton("  Play  ");
    JButton stop = new JButton("Stop");
    JButton reverse = new JButton("Reverse");
    JButton forward = new JButton("Forward");
    JButton shuffle = new JButton("Shuffle");
    JPanel pd = new JPanel();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel_file = new JPanel();
    JFileChooser filechooser = new JFileChooser();
    JMenuBar menu = new JMenuBar();
    JMenu datei = new JMenu("Datei");      
    JMenuItem laden = new JMenuItem("Load");
    static String[] arr = {"C:/handy.wav","C:/Flogging Molly - Black Friday Rule.wav","C:/Millencolin - No Cigar.wav","C:/Kopie von Big tymers - bling bling.wav"};
    static JList list = new JList(arr);
    JSlider lautstaerke = new JSlider(JSlider.VERTICAL,0,100,50);
    ListSelectionModel ListSelectionModel= list.getSelectionModel();
    File currentFile;
    public boolean running;
    static Clip clip;
    
    
    int i = list.getSelectedIndex();
    String pfad= arr[i+1];
    //File file = new File();
    
    public MusicPlayer(){
        super("MusicPlayer");
        this.setSize(450,300);
        this.setLayout(new BorderLayout());
        panel1.setLayout(new FlowLayout());
        panel1.add(reverse);
        panel1.add(play);
        panel1.add(stop);
        panel1.add(forward);
        panel2.add(lautstaerke);
        panel3.add(shuffle);
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        ListSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
        laden.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent f){
                final String cmd = f.getActionCommand();
                if (cmd.equals("Load")){
                    FileDialog();
                }
            }
        });
        forward.addActionListener(this);
	reverse.addActionListener(this);
	stop.addActionListener(this);
        play.addActionListener(this);
		
        this.add(panel1,BorderLayout.SOUTH);
        this.add(panel2,BorderLayout.EAST);
        this.add(list, BorderLayout.CENTER);
        this.add(menu, BorderLayout.NORTH);
        this.add(panel3,BorderLayout.WEST);
        menu.add(datei);
        datei.add(laden);
        lautstaerke.setPaintTicks(true);
        lautstaerke.setMinorTickSpacing(3);
        filechooser.setFileHidingEnabled(false);
        this.setVisible(true);
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        Player(pfad);
       
        setVisible(true);
	
    }
    
    
    public void actionPerformed(ActionEvent e) {
                           final String cmd = e.getActionCommand();
                          
                           new Thread(){
                                public void run(){
                                   if(cmd.equals("  Play  ")){
                                    if (!running) {				
                                        running = true;
					//Player(pfad);
                                        clip.start();
                                        play.setText("Pause");
                                        list.setSelectedIndex(i+1);
                                        }} else if(cmd.equals("Pause")) {
					running = false;
					clip.stop();
                                        play.setText("  Play  ");
				}}
                                
			}.start();
                            new Thread(){
                                public void run(){
                                    if(cmd.equals("Forward")){
                                        clip.stop();
                                        pfad=arr[i+2];
                                        Player(pfad);
                                        clip.start();
                                        list.setSelectedIndex(i+2);
                                }
                                }
                        }.start();
                           new Thread(){
                                public void run(){
                                    if(cmd.equals("Back")){
                                        clip.stop();
                                        pfad=arr[i+1];
                                        Player(pfad);
                                        clip.start();
                                        //list.setSelectedIndex(i);
                                }
                                }
                        }.start();
                            new Thread(){
                                public void run(){
                                    if(cmd.equals("Stop")){
                                        clip.stop();
                                        clip.drain();
                                        Player(pfad);
                                        running = false;
                                        clip.stop();
                                        play.setText("  Play  ");
                                }
                                }
                        }.start();
                          }	
    
        ;
        
    private void Player(String pfad){   
        
        try{
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File(pfad)); 
                            AudioFormat format = ais.getFormat();
                            DataLine.Info info = new DataLine.Info(Clip.class, format,
					((int) ais.getFrameLength() * format.getFrameSize()));
                            if(!AudioSystem.isLineSupported(info)) {
                                System.out.println("Das Format wird nicht unterstützt: " + info);
                                return;
                                }
			clip = (Clip) AudioSystem.getLine(info);
			clip.open(ais);

			FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
			gainControl.setValue(1.0F);
            } 
        catch (Exception e) {
			//e.printStackTrace();
		}
	}
                                    
    
                        
    class SharedListSelectionHandler implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) {
        System.out.println(arr [list.getSelectedIndex()]);
    }
}
            
        
    
    public static void main(String[] args) {
        new MusicPlayer();
    }
    public void FileDialog(){
		filechooser.setVisible(true);
		int retValue = filechooser.showOpenDialog(pd);
    }
    
      

}


Wenn ich das Programm jetzt starte und play drücke bekomm ich folgende Fehlermeldung:
Exception in thread "Thread-2" java.lang.NullPointerException
at MusicPlayer$2.run(MusicPlayer.java:110)

Konnte ein Stück auch schon abspielen, aber da hatte ich noch keine Threads benutzt. Jetzt gehts nicht mehr

Kann mir jemand sagen, was diese Ecxeption sagt (bin beim googlen nicht schlau geworde) bzw. was ich falsch mache

Liegts an der Benutzung der Threads oder vll an der Datei selber (sind alles .wav-Files)
Will erstmal dieses Problem bereinigen, bevor ich weiterprogrammier

Vielen dank für jede Antwort

Der Claude
 
S

SlaterB

Gast
das Problem ist, dass irgendein Objekt null ist,

als erstes musst du herausfinden, welche Programmzeile gemeint ist,
nach der Nummerierung hier ist es eine leere Zeile

es scheint eines der Objekte clip, play oder list zu sein,
nun musst du in deinem Programmverlauf überprüfen, wann diese Objekte gesetzt und vielleicht wieder auf null gesetzt werden usw.

idealerweise baust du dein Programm zu einem Log-Programm um a la

System.out.println("Clip wird gesetzt mit Objekt ..");
System.out.println("Thread .. läuft an, greift auf Clip .. zu.");
usw.
 
N

nirvana002

Gast
Einfach debuggen und dann weist du ganz genau was null ist.
 

Siassei

Bekanntes Mitglied
Hallo,

eines der folgenden Objekte hast du wohl zu diesem Zeitpunkt nicht instanziert
Code:
                                        clip.start();
                                        play.setText("Pause");
                                        list.setSelectedIndex(i+1);
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben