尝试在Java程序中播放音乐

时间:2012-04-25 14:36:28

标签: java url applet

我正在尝试使用从我的硬盘驱动器播放声音文件的测试程序,但我继续得到NullPointerException。这是迄今为止的代码,我主要从dream in code中删除:

package ForSelf;

import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.net.*;

public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
  private AudioClip song; // Sound player
  private URL songPath; // Sound path

  Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL
        song = Applet.newAudioClip(songPath); // Load the Sound
     }catch(Exception e){
         e.printStackTrace();
         //e.getMessage();
     } // Satisfy the catch
  }

  public void playSound(){
     song.loop(); // Play 
  }
  public void stopSound(){
     song.stop(); // Stop
  }
  public void playSoundOnce(){
     song.play(); // Play only once
  }
}

public void init(){
  Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
  Container c = getContentPane();

  c.setBackground(Color.white);
  c.setLayout(null);

  playerPanel = new JPanel();
  playSound = new JButton("Play");
  stopSound = new JButton("Stop");

  playerPanel.add(playSound);
  playerPanel.add(stopSound);
  c.add(playerPanel);

  testsong.playSound();
}

public void paint(Graphics g){
    super.paint(g);
    playerPanel.setLocation(0, 0);
    playerPanel.setSize(300, 300);
}

public void actionPerformed(ActionEvent e){

}
}

稍后将实现JComponents以播放和停止歌曲文件 我不确定这只是我的文件路径还是什么,所以任何帮助都会受到赞赏。

更新的代码如下:

package ForSelf;

import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.*;
import java.net.*;

public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
  private AudioClip song; // Sound player
  private URL songPath; // Sound path

  Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL
        song = Applet.newAudioClip(songPath); // Load the Sound
     }catch(Exception e){
         e.printStackTrace();
         //e.getMessage();
     } // Satisfy the catch
  }

  public void playSound(){
     song.loop(); // Play 
  }
  public void stopSound(){
     song.stop(); // Stop
  }
  public void playSoundOnce(){
     song.play(); // Play only once
  }
}

public void init(){
    **String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
    String puzzleSolutionGet = directory + "PuzzleSolutionGet";
    Sound testsong = new Sound(puzzleSolutionGet);**

  //Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");

  Container c = getContentPane();

  c.setBackground(Color.white);
  c.setLayout(null);

  playerPanel = new JPanel();
  playSound = new JButton("Play");
  stopSound = new JButton("Stop");

  playerPanel.add(playSound);
  playerPanel.add(stopSound);
  c.add(playerPanel);

  testsong.playSound();
}

public void paint(Graphics g){
    super.paint(g);
    playerPanel.setLocation(0, 0);
    playerPanel.setSize(300, 300);
}

public void actionPerformed(ActionEvent e){

}
}

但是当我运行它时,我得到了以下例外:

java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at ForSelf.AudioTest$Sound.<init>(AudioTest.java:23)
    at ForSelf.AudioTest.init(AudioTest.java:45)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)
java.lang.NullPointerException
    at ForSelf.AudioTest$Sound.playSound(AudioTest.java:32)
    at ForSelf.AudioTest.init(AudioTest.java:62)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)

这可能比以前更有用。我甚至将文件移动到桌面以便更快地访问,但我确定位置不是问题。

2 个答案:

答案 0 :(得分:1)

您需要使用正斜杠而不是反斜杠。我使用linux路径名(“/home/username/song.wav”)在我的linux机器上运行你的代码,它没有抛出任何异常(只是一个警告)。

我很确定你需要使用:

Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");

请记住,windows接受正斜杠和反斜杠作为目录分隔符。

如果这对您不起作用,请尝试使用System.getProperty(“user.dir”)和System.getProperty(“file.separator”)方法创建当前目录的String,然后只需要在最后连接你的文件名:

String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
String puzzleSolutionGet = directory + "PuzzleSolutionGet";
Sound testsong = new Sound(puzzleSolutionGet);

这样您的代码就可以在任何操作系统上运行。

它还可以节省大量时间来切换您认为引发异常的行并尝试调试代码,逐步进入每一行,直到看到抛出异常为止。如果您还没有使用调试器,那么这是IBM的一个很酷的链接,解释了基础知识:

http://www.ibm.com/developerworks/library/os-ecbug/

答案 1 :(得分:0)

Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
...
Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL

路径需要相对于codebase,并使用正斜杠/。例如。如果codebase指向:

"C:\\Users\\MyName\\lib"

构造函数看起来应该更像:

Sound testsong = new Sound("../Music/PuzzleSolutionGet.wav");