简体   繁体   English

如何在Java中使用AudioSystem播放音乐(.mp3)?

[英]How to play Music (.mp3) in Java with AudioSystem?

I made this the classes given below to implement sounds in a game. 我按照以下给出的类在游戏中实现声音。

On execution i get the following error. 执行时,出现以下错误。

Please can some-one tell me why i'm getting this error and how to solve it!! 请有人可以告诉我为什么会出现此错误以及如何解决!!

This is my Sound-Class file: 这是我的声音类文件:

package flappyLemon.model.game;

import javax.sound.sampled.*;

public class Sound {

   private Clip clip;

   public static Sound sound = new Sound("LemonTree.mp3");

   public Sound(String fileName) {
         try {
                AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));

                clip = AudioSystem.getClip();
                clip.open(ais);
         } catch (Exception e) {
                e.printStackTrace();
         }
   }

   public void play() {
         try {
                if (clip != null) {
                       new Thread() {
                              public void run() {
                                    synchronized (clip) {
                                           clip.stop();
                                           clip.setFramePosition(0);
                                           clip.start();
                                    }
                              }
                       }.start();
                }
         } catch (Exception e) {
                e.printStackTrace();
         }
   }
}

Now i call it: 现在我称之为:

Sound.sound.play();

And then I became a NullPointerException: 然后我变成了NullPointerException:

java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:207)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:841)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:178)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1147)
at flappyLemon.model.game.Sound.<init>(Sound.java:13)
at flappyLemon.model.game.Sound.<clinit>(Sound.java:9)
at flappyLemon.model.FlappyLemon.main(FlappyLemon.java:67)

Copy your audio source file into your project 将音频源文件复制到项目中

like this: 像这样:
屏幕截图示例

Java api does not allow .mp3 files . Java api不允许.mp3文件。 you should use .wav files. 您应该使用.wav文件。

private Clip clip;

   public static Sound sound = new Sound("Yamaha-TG100-Whistle-C5.wav");

   public Sound(String fileName) {
         try {
                AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));

                clip = AudioSystem.getClip();
                clip.open(ais);
         } catch (Exception e) {
                e.printStackTrace();
         }
   }

for more information click here 欲了解更多信息, 请点击这里

Java doesn't Support mp3 files, mp3 is a Container for Audio, which most be encoded to use. Java不支持mp3文件,mp3是音频容器,大多数已编码为使用。

Quote from developer.com: 来自developer.com的报价:

Java Sound supports a wide variety of file types including AIFF, AU, and WAV. Java Sound支持多种文件类型,包括AIFF,AU和WAV。 It can render both 8- and 16-bit audio data in sample rates from 8 KHz to 48 KHZ. 它可以以8 KHz至48 KHZ的采样率呈现8位和16位音频数据。

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

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