在java中播放mp3文件 - nullpointerexception

时间:2015-12-06 13:12:32

标签: java audio

我正在创建一个带有一些背景音乐的应用。我正试着用这堂课演奏这种音乐:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class SoundPlayer {

    public static void playSound() {
        try{
              AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(SoundPlayer.class.getResource("/Music.mp3"));
             Clip clip = AudioSystem.getClip();
             clip.open(audioInputStream);
             clip.start( );
            }
           catch(Exception ex) {
               ex.printStackTrace();
           }

    }
}

但是我在第9行得到了一个nullpointerexception,它是AudioInputStream。 我不知道如何访问此文件: Music.mp3

我试过了:

SoundPlayer.class.getResource("/Music.mp3")
SoundPlayer.class.getResource("Music/Music.mp3")
SoundPlayer.class.getResource("./Music/Music.mp3")

当我把它放在我的文件的根目录中并使用它时:

SoundPlayer.class.getResource("Music.mp3")

我收到此错误: javax.sound.sampled.UnsupportedAudioFileException:无法从输入URL获取音频输入流 的更新 我尝试用它来处理JavaFX:

package com.audio;

import java.nio.file.Paths;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class PlayAudio {

    public static void PlayMP3() {
        Media media = new Media(Paths.get("Music/Music.mp3").toUri().toString());
        MediaPlayer player = new MediaPlayer(media);
        player.play();
    }
} 

但是我收到了错误:

Exception in thread "Thread-2" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at javafx.application.Platform.runLater(Unknown Source)
    at javafx.scene.media.Media$_MetadataListener.onMetadata(Unknown Source)
    at com.sun.media.jfxmediaimpl.MetadataParserImpl.done(Unknown Source)
    at com.sun.media.jfxmediaimpl.platform.java.ID3MetadataParser.parse(Unknown Source)
    at com.sun.media.jfxmediaimpl.MetadataParserImpl.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at javafx.application.Platform.runLater(Unknown Source)
    at javafx.scene.media.MediaPlayer.init(Unknown Source)
    at javafx.scene.media.MediaPlayer.<init>(Unknown Source)
    at com.audio.PlayAudio.PlayMP3(PlayAudio.java:12)
    at com.Frame1$2.actionPerformed(Frame1.java:61)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)

此行中的错误:MediaPlayer player = new MediaPlayer(media);如何初始化工具包?

1 个答案:

答案 0 :(得分:2)

尝试将文件放在根目录中。也许您可以使用MainClass.getResourceAsStream("file.mp3");

从那里访问它

编辑:使用.wav文件。