繁体   English   中英

创建多线程音乐播放器

[英]creating a multithreaded music player

我对创建多线程程序非常陌生(这是我第一次应用它),我将 Runnable 实现到我的班级,并在我的一个按钮 Actionlistener 中运行它(new Thread(new Project()).start() ,但是我导入的每个资源都被删除了。就像文件一样。它只是写了 NullPointerException ,我什至测试了一个字符串,但它的结果返回 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();
     }

您正在设置类中的所有变量,但单击按钮会创建此类的新实例。 这个新实例对旧变量的内容一无所知。 它们都被再次初始化。

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

您可能应该创建一个实现可运行的新类。 为该类提供所有需要的引用(可能通过构造函数),然后启动该类。

这可能是您的播放器类

在你的 TestMT 中调用它

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();
    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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