[英]Playing a mp3 in java
I am working on a Java project that involves playing mp3 files. 我正在一个涉及播放mp3文件的Java项目中。 I want my application to play the files from within the project so I have the music files stored in a folder called music which is in a source folder called resources. 我希望我的应用程序从项目中播放文件,因此我将音乐文件存储在一个名为music的文件夹中,该文件夹位于一个称为resources的源文件夹中。 This is the code I have right now but when I run it I get a Bitstream errorcode 102 . 这是我现在拥有的代码,但是当我运行它时,我得到一个Bitstream错误代码102 。 I can't seem to figure out what is wrong, any help? 我似乎无法弄清楚出什么问题了,有什么帮助吗? I am using the javazoom library (javazoom.jl.player.Player) 我正在使用javazoom库(javazoom.jl.player.Player)
public void play() {
try {
InputStream stream = MP3.class.getClassLoader()
.getResourceAsStream("/music/LoveStory.mp3");
BufferedInputStream bis = new BufferedInputStream(stream);
player = new Player(bis);
} catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try {
player.play();
} catch (Exception e) {
System.out.println(e);
}
}
}.start();
}
I actually discovered my problem and it is such a small thing I am quite embarrassed! 我实际上发现了我的问题,这太小了,我很尴尬! It turns out when I removed the leading "/" in front of the path name my code ran fine. 原来,当我删除路径名前面的前导“ /”时,我的代码运行正常。 I believe this had to do with an absolute path vs a relative path. 我认为这与绝对路径与相对路径有关。
Use the JavaFX environment: 使用JavaFX环境:
package be.saleconix;
import java.net.URL;
import com.sun.javafx.application.PlatformImpl;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
class AudioPlayer implements Runnable {
private URL resource;
private Media media;
private MediaPlayer mediaPlayer;
private boolean ready = false;
private Thread t;
private String threadname;
private static Integer numberOfActiveThreads = 0;
/**
* Start a mediaplayer thread
* @param filename
* @param threadname
* @throws NullPointerException
* @throws InterruptedException
*/
public AudioPlayer(String filename, String threadname) throws NullPointerException, InterruptedException {
if (numberOfActiveThreads == 0) {
PlatformImpl.startup(()->{}); // initialize JavaFX environment
System.out.println("The JavaFX environment has been started");
}
this.threadname = threadname;
resource = getClass().getResource("/" + filename);
media = new Media(resource.toString());
t = new Thread(this, threadname);
t.start();
numberOfActiveThreads ++;
}
public void run() {
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setOnReady(() -> { // register a callback and let it deregister itself after execution
System.out.println("The mediaplayer [" + threadname + "] has been started");
ready = true;
mediaPlayer.setOnReady(null);
});
while (!t.isInterrupted());
System.out.println("The mediaplayer [" + threadname + "] has been terminated");
}
String getThreadname() {
return threadname;
}
/**
* Plays the sound from the start
* @throws InterruptedException
*/
void play() throws InterruptedException {
play(0);
}
/**
* Plays the sound at a specific position in ms
* @throws InterruptedException
* @throws IllegalThreadStateException
*/
void play(double position) throws InterruptedException, IllegalThreadStateException {
int i = 500;
while (!ready && i > 0) {
Thread.sleep(1);
i--;
}
if (i > 0) {
mediaPlayer.setStartTime(Duration.millis(position));
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
mediaPlayer.setOnStopped(()->{
mediaPlayer.play();
mediaPlayer.setOnStopped(null);
});
mediaPlayer.stop();
} else {
mediaPlayer.play();
}
} else {
throw new IllegalThreadStateException("The mediaplayer is not ready after 500ms timeout");
}
}
/**
* Stop the mediaplayer thread
*/
void stop() {
mediaPlayer.setOnReady(null);
mediaPlayer.setOnStopped(null);
mediaPlayer.stop();
numberOfActiveThreads --;
t.interrupt();
if (numberOfActiveThreads == 0) {
PlatformImpl.exit(); // exit the JavaFX environment
System.out.println("The JavaFX environment has been exited");
}
}
}
public class Main {
public static void main(String[] args) {
try {
AudioPlayer audioPlayerA = new AudioPlayer("test.mp3", "audioplayerA");
System.out.println("Play audioplayerA from start");
audioPlayerA.play();
System.out.println("Waiting 10s ...");
Thread.sleep(10000);
System.out.println("Play audioplayerA at position 20s");
audioPlayerA.play(20000);
System.out.println("Waiting 5s ...");
Thread.sleep(5000);
AudioPlayer audioPlayerB = new AudioPlayer("test1.mp3", "audioplayerB");
System.out.println("Play audioplayerB from start.");
audioPlayerB.play();
System.out.println("Waiting 20s ...");
Thread.sleep(20000);
System.out.println("Stopping audioplayerA");
audioPlayerA.stop();
System.out.println("Stopping audioplayerB");
audioPlayerB.stop();
} catch (NullPointerException e) {
System.out.println("Cannot create audio player, check filename.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e);
} finally {
System.out.println("Main thread has been exited");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.