简体   繁体   中英

creating a multithreaded music player

im very new to creating multithreaded programs , (this is the first time i apply it) , i implement Runnable to my class , and in one of my buttons Actionlistener i run it (new Thread(new Project()).start() , but every resource i import are deleted. like the file . it just write NullPointerException , i even tested a string , but its result returns to null

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        chooser = new JFileChooser();
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setCurrentDirectory(new File("."));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter filter = new FileNameExtensionFilter ("wav files","wav");
        chooser.setFileFilter(filter);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) 
        { 
            Song = chooser.getSelectedFile();
        }
        else
        {
        JOptionPane.showMessageDialog(null, "you didnt choose any file !!");
        }
    }            

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        (new Thread(new TestMT())).start();
    }      

public void run() {
            // TODO Auto-generated method stub
             try {
                    audioStream = AudioSystem.getAudioInputStream(Song);
                    audioFormat = audioStream.getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
                sourceLine = (SourceDataLine) AudioSystem.getLine(info);
                sourceLine.open(audioFormat);
            } catch (UnsupportedAudioFileException | IOException e1) {
                 e1.printStackTrace();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            }
        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            try {
                nBytesRead = audioStream.read(abData, 0, abData.length);
              } catch (IOException e) {
                  e.printStackTrace();
            }
             if (nBytesRead >= 0) {
                 @SuppressWarnings("unused")
                 int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

         sourceLine.drain();
         sourceLine.close();
     }

You are setting all the variables in your class but on button click you create a new instance of this class. This new instance doesn't know anything about the content of the old variables. They are all initialized again.

(new Thread(new TestMT())).start();

You should maybe create a new class that implements runnable. Give that class all the needed references (via constructor maybe) and then start that one.

This could be your player class

call it in your TestMT by

new Thread(new Player(song)).start();

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Player implements Runnable {

private File song;
private static int BUFFER_SIZE = 4096;

Player(File song) {
    this.song = song;
}

@Override
public void run() {
    try {
        AudioInputStream audioStream = AudioSystem
                .getAudioInputStream(song);
        AudioFormat audioFormat = audioStream.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                audioFormat);
        SourceDataLine sourceLine = (SourceDataLine) AudioSystem
                .getLine(info);
        sourceLine.open(audioFormat);
        sourceLine.start();
        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
        sourceLine.drain();
        sourceLine.close();
    } catch (UnsupportedAudioFileException | IOException e1) {
        e1.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM