英文:
Playing mp3s from the middle with or without JavaFX
问题
有没有一种方法可以在Java中播放一个mp3文件,并且从特定的起始点开始,而不使用JavaFX?我尝试了很多库,但是我能够让其工作的库只提供了开始、暂停、停止等基本功能。
我正在使用Eclipse。
编辑:我对JavaFX的经验:
-
我安装了e(fx)clipse。
-
我将所有的jar文件都添加到了构建路径(Build Path)中。
-
我将以下内容添加到了VM参数中:
--module-path "C:\my\path\to\External Libraries\javafx-sdk-14\lib" --add-modules javafx.controls,javafx.fxml
- Main类的main方法启动了JavaFX应用程序,之后启动start方法,该方法使用以下代码初始化了我的Game类:
package application;
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Game {
public Game() {
String songPath = "C:\\Users\\Dustin\\Meine Dateien\\Musik\\EDM\\1991 - Jungle Cats.mp3";
Media hit = new Media(new File(songPath).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
}
}
- 我得到了以下异常:
Exception in Application start method
java.lang.reflect.InvocationTargetException
...
Exception running application application.Main
英文:
Is there a way to play an mp3 in java from a specific starting point without using JavaFX? I tried many libraries but those that I can get to work don't provide functionalities beyond start, pause, stop.
I'm using Eclipse.
Edit: My experience with JavaFX:
-
I installed e(fx)clipse.
-
I added all their jars to the Build Path.
-
I added this to the VM Arguments:
--module-path "C:\my\path\to\External Libraries\javafx-sdk-14\lib" --add-modules javafx.controls,javafx.fxml
- Main's main method launches the javafx application and the start method afterwards, which initializes my Game class with the following code:
package application;
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Game {
public Game() {
String songPath = "C:\\Users\\Dustin\\Meine Dateien\\Musik\\EDM\\1991 - Jungle Cats.mp3";
Media hit = new Media(new File(songPath).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
}
}
- I'm getting the following exceptions:
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:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
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:567)
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:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module @0x69d159f0) cannot access class com.sun.glass.utils.NativeLibLoader (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.utils to unnamed module @0x69d159f0
at com.sun.media.jfxmediaimpl.NativeMediaManager.lambda$new$0(NativeMediaManager.java:110)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
at com.sun.media.jfxmediaimpl.NativeMediaManager.<init>(NativeMediaManager.java:107)
at com.sun.media.jfxmediaimpl.NativeMediaManager$NativeMediaManagerInitializer.<clinit>(NativeMediaManager.java:78)
at com.sun.media.jfxmediaimpl.NativeMediaManager.getDefaultInstance(NativeMediaManager.java:90)
at com.sun.media.jfxmedia.MediaManager.canPlayProtocol(MediaManager.java:78)
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:239)
at javafx.scene.media.Media.<init>(Media.java:393)
at application.Game.<init>(Game.java:18)
at application.Main.start(Main.java:21)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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:174)
... 1 more
Exception running application application.Main
答案1
得分: 0
如果您的库提供了允许您每个缓冲区读取特定数量帧的功能,一种到达特定时间的方法是在不播放的情况下进行读取,同时计算相应数量的帧数,并丢弃数据。这种方法有时可行,因为音频处理要比播放数据快得多。延迟将与您需要跳过的音乐量成比例,以便到达所需位置。
另一种策略是将整个文件加载到内存中,并使用Clip的定位功能。如果您处理的流很长,这种方法可能不可行。
您有不使用JavaFX的特定原因吗?
英文:
If your library exposes functionality that allows you to read a specific number of frames per buffer, a way to get to a particular time is to read without playback while counting the corresponding number of frames, discarding the data. This sometimes works because audio processing is much faster than the playback of that data. The lag will be proportional to the amount of music you have to skip over to get to the desired spot.
Another strategy is to load the entire file into memory and use the positioning power of a Clip. This can be unworkable if you are dealing with a stream that is many minutes long.
Any particular reason why you don't want to use JavaFX?
专注分享java语言的经验与见解,让所有开发者获益!
评论