我试图在java中实现mp3播放器,它将播放来自字节数组的音频。
课程应该像这样工作:
关键是我使用jLayer的实现不起作用:当我播放 - 暂停 - 恢复时,音频暂停但随后从文件中的随机点恢复。
public class MPlayer{
AdvancedPlayer player;
private static int pausedOnFrame =0;
private byte[] decrypted = null;
private long audioLength;
private AudioInputStream stream;
private InputStream bytesToStream(byte[] in) {
InputStream is = new ByteArrayInputStream(in);
return is;
}
public MPlayer(String fname) {
/* here file is encrypted to variable byte[] decrypted and then: */
InputStream is = bytesToStream(decrypted);
stream = AudioSystem.getAudioInputStream(is);
audioLength = stream.getFrameLength();
player = new AdvancedPlayer(stream);
player.setPlayBackListener(new PlaybackListener() {
@Override
public void playbackFinished(PlaybackEvent event) {
System.err.println(event.getFrame());
pausedOnFrame = event.getFrame();
}
}
}
public void play() throws Exception {
Thread th = new Thread() {
public void run() {
player.play(MPlayer.pausedOnFrame, Integer.MAX_VALUE);
}
};
th.start();
}
public void fastforward() {
pausemusic();
long nextFrame = (long) (pausedOnFrame+0.02*audioLength);
if (nextFrame < audioLength)
play();
}
public void rewind() {
pausemusic();
long nextFrame = (long) (pausedOnFrame-0.02*audioLength);
if (nextFrame > 0)
play();
}
}
public void pausemusic() throws LineUnavailableException {
player.stop();
}
public void stopmusic() throws LineUnavailableException {
player.stop();
pausedOnFrame = 0;
}
}
我该如何解决?
答案 0 :(得分:0)
您似乎必须在暂停方法上调用stop而不是给它一个位置。
public void pausemusic() throws LineUnavailableException {
player.stop();
}
public void stopmusic() throws LineUnavailableException {
player.stop();
pausedOnFrame = 0;
}
我不熟悉你的代码是如何工作的,但是如果'stopmusic'方法通过'pausedOnFrame = 0'将歌曲重置为0,你为什么不在播放期间保留一个计数器以便你可以创建一个知道自己定位的暂停方法?
简而言之,为什么不跟踪歌曲的位置?