I'm trying to implement mp3 player in java that will play audio from array of bytes.
The class should work like this:
The point is that my implementation using jLayer is not working: when I do play - pause - resume, the audio is being paused but then resumed from random point in file.
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;
}
}
How may I fix it?
You seem to have to be calling stop on your pause method and not giving it a location.
public void pausemusic() throws LineUnavailableException {
player.stop();
}
public void stopmusic() throws LineUnavailableException {
player.stop();
pausedOnFrame = 0;
}
I'm not familiar with how your code works, but if the 'stopmusic' method resets the song to 0 via the 'pausedOnFrame = 0', why don't you keep a counter during the play so that you can create a pause method that knows where to locate itself?
In short, why aren't you tracking where the song is?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.