簡體   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