繁体   English   中英

尝试在Java程序中播放音乐

[英]Trying to play music in Java program

我正在尝试运行一个测试程序,该程序可以从硬盘驱动器播放声音文件,但仍会继续获取NullPointerException 这是到目前为止的代码,我主要是在代码中梦想中摘下来的:

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)

这可能比以前提供了更多信息。 我什至将文件移到桌面上以加快访问速度,但是我确定位置不是问题。

您需要使用正斜杠而不是反斜杠。 我使用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/

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

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

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

构造函数应更像:

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

暂无
暂无

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

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