本文介绍了jlayer.javazoom播放器无法停止mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在jlayer中停止MP3? (the stop()
不再使用)
How to stop MP3 in jlayer? (the stop()
is no longer used)
我的代码如下:
//main class mp3_main
private AdvancedPlayer player;
public static void main(String[] args) {
String file="C:\\cd.mp3";
mp3PlayerSample mp3 = new mp3PlayerSample(file);
mp3.play();
mp3.stop();
}
//class mp3PlayerSample
private String filename;
private BufferedInputStream buffer;
private AdvancedPlayer player;
//constructor
public mp3PlayerSample(String filename)
{
this.filename = filename;
}
//start method
public void play()
{
FileInputStream fis;
try {
fis = new FileInputStream(this.filename);
buffer = new BufferedInputStream(fis);
try {
this.player=new AdvancedPlayer(buffer);
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//stop method
public void stop()
{
if(player != null){
player.close();
}
}
推荐答案
您需要在其自己的线程中运行播放器,现在您的主要方法在调用play()之后会阻塞,直到播放完成.
You need to run the player in its own thread, right now your main method blocks after calling play() until playback has completed.
注意,jlayer附带的Player/AdvancedPlayer类是 example 代码,用于演示如何连接解码音频的解码和输出.他们不是完全成熟的玩家(例如,甚至没有音量控制).
Note, the classes Player/AdvancedPlayer included with jlayer are meant as example code to demonstrate how the decoding and output of decoded audio has to be wired up. They are not fully fledged players (e.g. there isn't even volume control).
这篇关于jlayer.javazoom播放器无法停止mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!