package com.first.serious.main.controls;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.sound.sampled.*;
public class AudioPlayer {
private Clip clip;
public AudioPlayer(String path){
try{
AudioInputStream AIS = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(path));
InputStream bufferedIn = new BufferedInputStream(AIS);
AudioInputStream audiostream = AudioSystem.getAudioInputStream(bufferedIn);
/* AudioFormat baseFormat = AIS.getFormat();
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,baseFormat.getChannels(),
baseFormat.getChannels()*2,
baseFormat.getSampleRate(),false);
AudioInputStream DAIS = AudioSystem.getAudioInputStream(decodeFormat,AIS);*/
clip = AudioSystem.getClip();
clip.open(audiostream);
}catch(Exception e){
e.printStackTrace();
}
}
public void play(){
if(clip == null) return;
stop();
clip.setFramePosition(0);
clip.start();
}
public void stop(){
if(clip.isRunning()) clip.stop();
}
public void close(){
stop();
clip.close();
}
}
老实说,我刚刚复制了代码,因为我是游戏开发的完全初学者。已经有两组代码,注释行来自第一组。
我在这个类中用于mouseinputs
package com.first.serious.main.controls;
import com.first.serious.main.Shooter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseInput implements MouseListener{
private Shooter game;
private AudioPlayer deadsound;
public MouseInput(Shooter game){
this.game = game;
deadsound = new AudioPlayer("/res/sounds/deadsound2.mp3");
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
int mx = me.getX();
int my = me.getY();
//System.out.println(mx + " " + my);
if(game.state.equals("menu")){
if(mx > 60 && mx < 330 && my > 200 && my < 280){
deadsound.play();
game.state = "game";
}else
if(mx > 60 && mx < 330 && my > 460 && my < 540){
System.exit(0);
}else
if(mx > 60 && mx < 330 && my > 330 && my < 410){
game.state = "help";
}
}
if(game.state.equals("Dead")){
// System.out.println("dead");
if(mx > 60 && mx < 330 && my > 260 && my < 340){
game.Score = 0;
game.state = "game";
}else
if(mx > 60 && mx < 330 && my > 390 && my < 470){
System.exit(0);
}
}
if(game.state.equals("Upgrade")){
if(mx > 60 && mx < 330 && my > 200 && my < 280){
game.upgradectr = 0;
game.player.XX += 0.5f;
game.state2++;
game.state = "game";
}else
if(mx > 60 && mx < 330 && my > 330 && my < 410){
game.upgradectr = 0;
game.getControl().Vy -= 1f;
game.state2++;
game.state = "game";
}else
if(mx > 60 && mx < 330 && my > 460 && my < 540){
if(game.state2==05||game.state2==10||game.state2==20){
game.upgradectr = 0;
game.player.a += 2;
game.state2++;
game.state = "game";
}else{
game.upgradectr = 0;
System.out.println(game.upgradectr);
game.getControl().reload -= 2;
game.state2++;
System.out.println(game.state2);
game.state = "game";
}
}
}else
if(game.state.equals("help")){
if(mx > 60 && mx < 330 && my > 460 && my < 540){
game.state = "menu";
}
}
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
}
然后得到这些错误
java.lang.NullPointerException
at javazoom.spi.mpeg.sampled.file.MpegAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1113)
at com.first.serious.main.controls.AudioPlayer.<init>(AudioPlayer.java:17)
at com.first.serious.main.controls.MouseInput.<init>(MouseInput.java:15)
at com.first.serious.main.Shooter.init(Shooter.java:75)
at com.first.serious.main.Shooter.run(Shooter.java:670)
at java.lang.Thread.run(Thread.java:748)
我不完全知道确切的问题是什么。我有以下jar库:mp3spi1.9.5.jar,jl1.0.1.jar和tritonus_share.jar。请帮忙。谢谢。
请参阅目录链接以供参考。谢谢!。现在意识到这个问题,但还没有完全理解。 我很抱歉,但我现在的问题是,当我的java文件在controls文件夹中时,如何从声音文件夹中访问文件。我尝试将文件放在java文件所在的位置并且工作正常。但我希望我的文件能够尽可能地组织起来。请帮忙谢谢。