简体   繁体   English

无法使用 Javafx MediaPlayer 播放 wav 或 mp3 文件。 也不能用原生的java库播放wav文件

[英]Can't play wav or mp3 files with Javafx MediaPlayer. Also can't play wav files with the native java library

I have a java project where I have to make a music player that plays either wav or mp3 files.我有一个 java 项目,我必须制作一个播放 wav 或 mp3 文件的音乐播放器。 However I can't get my wav or mp3 files to play using the Javafx libraries or with native java libraries.但是,我无法使用 Javafx 库或本机 java 库播放我的 wav 或 mp3 文件。 I've checked and made sure the wav and mp3 files I'm using to test aren't corrupted.我已经检查并确保我用来测试的 wav 和 mp3 文件没有损坏。 I'm using Javafx 17.0.2 and JDK 11.我正在使用 Javafx 17.0.2 和 JDK 11。

Mini Reproducible Example With Javafx带有 Javafx 的迷你可复制示例

JavaFxMp3WavPlayer JavaFxMp3WavPlayer

package mediaplayerjavafx;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class JavaFxMp3WavPlayer extends Application {

    public static void main(String[] args) throws MalformedURLException, IOException {
        launch(args);

    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("My");
        Button button = new Button("My Button");
        Scene scene = new Scene(button, 200, 100);
        stage.setScene(scene);
        stage.show();
        File file = new File("C:\\Users\\John Doe\\MotisHarmony\\accounts\\yourLieInApril\\downloadedMusic\\Mp3Test.mp3");
        String path = file.toURI().toASCIIString();
        Media media = new Media(path);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        button.setOnAction(new EventHandler() {
            @Override
            public void handle(Event arg0) {
                runMusicPlayer(mediaPlayer);
            }
        });
    }

    public void runMusicPlayer(MediaPlayer mediaPlayer) {
        mediaPlayer.play();
    }
}

module-info模块信息

module MotisHarmony {
    requires javafx.swt;
    requires javafx.base;
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.graphics;
    requires javafx.media;
    requires javafx.swing;
    requires javafx.web;
    exports mediaplayerjavafx;
    opens mediaplayerjavafx to javafx.graphics;
}

Error Produced产生错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.media/javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:519)
    at javafx.media/javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:422)
    at MotisHarmony/mediaplayerjavafx.JavaFxMp3WavPlayer.start(JavaFxMp3WavPlayer.java:37)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:297)
    at javafx.media/com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.media/javafx.scene.media.MediaPlayer.init(MediaPlayer.java:475)
    ... 11 more
Exception running application mediaplayerjavafx.JavaFxMp3WavPlayer

So after this didn't work, I tried it with native Java libraries.因此,在这不起作用之后,我尝试使用本机 Java 库。

Mini Reproducible Example With Native Java Libraries使用本机 Java 库的迷你可重现示例

JavaWavPlayer JavaWavPlayer

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class JavaWavPlayer {

    /**
     * @param args the command line arguments
     */
    public static Clip clip;

    public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
        File yourFile = new File("C:\\Users\\John Doe\\MotisHarmony\\accounts\\yourLieInApril\\downloadedMusic\\WavTest.wav");
        AudioInputStream stream;
        AudioFormat format;
        DataLine.Info info;
        stream = AudioSystem.getAudioInputStream(yourFile);
        format = stream.getFormat();
        info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
    }

}

Error Produced产生错误

Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
    at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
    at javawavplayer.JavaWavPlayer.main(JavaWavPlayer.java:33)
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\John Doe\Dropbox(Old)\My PC (DESKTOP-P6JNU2B)\Documents\NetBeansProjects\JavaWavPlayer\nbproject\build-impl.xml:936: Java returned: 1

Extra Information额外的信息

Link to the Mp3 file I used to test.链接到我用来测试的 Mp3 文件。 https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing https://drive.google.com/file/d/1CvAafbMviQ7nvKyojnem9GK73LJsD6MJ/view?usp=sharing

Link to the Wav file I used to text.链接到我用来发短信的 Wav 文件。 https://drive.google.com/file/d/1k7a93pZIGY65sGs8BrFMDgeRrgYc0C5k/view?usp=sharing https://drive.google.com/file/d/1k7a93pZIGY65sGs8BrFMDgeRrgYc0C5k/view?usp=sharing

I am using JDK 11 and Javafx 17.0.2我正在使用 JDK 11 和 Javafx 17.0.2

System Type: 64-bit operating system, x64-based processor系统类型:64 位操作系统,基于 x64 的处理器

Processor: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz处理器:Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz 2.81 GHz

Windows Edition: Windows 10 Home Windows版:Windows 10首页

I could not reproduce your issue.我无法重现您的问题。

Your issue is environmental, exactly what it is I could not say.你的问题是环境问题,具体是什么我不能说。

I advise creating a new project in idea and following the same steps I did and it should work as long as you have the file path correct.我建议在 idea 中创建一个新项目并按照我所做的相同步骤进行操作,只要文件路径正确,它就应该可以工作。

These are the steps I followed to get allow the media to play with your sample app:这些是我为允许媒体使用您的示例应用程序播放而遵循的步骤:

  1. Created a new JavaFX project in Idea with OpenJDK 17.0.2 and JavaFX 17.0.2 on Windows 11.在 Windows 11 上使用 OpenJDK 17.0.2 和 JavaFX 17.0.2在 Idea 中创建了一个新的 JavaFX 项目

  2. Copy-and-pasted your JavaFX sample code into the new project.将 JavaFX 示例代码复制并粘贴到新项目中。

  3. Followed the instructions to add media handling to the project:按照说明将媒体处理添加到项目中:

  4. Downloaded your mp3 and wav files.下载了您的 mp3 和 wav 文件。

  5. Set the file path to each in turn.依次为每个设置文件路径。

  6. Ran the app and hit the play button for each file.运行应用程序并点击每个文件的播放按钮。

Both the MP3 and WAV files played without problem. MP3 和 WAV 文件都可以正常播放。

Your lie in april is nice, I will try to learn it.你四月的谎言很好,我会努力学习的。

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

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