我发现这个代码用于在java中播放mp3,我想在网络中使用UDP传输这个代码,所以我想将mp3文件转换为byte,这样我就可以使用DatagramPacket
发送它。我该怎么转换才能做到这一点?
我需要向datagrampacket
发送60Kbyte的缓冲区。
import javazoom.jl.player.advanced.*;
public class JLayerTest
{
public static void main(String[] args)
{
SoundJLayer soundToPlay = new SoundJLayer("Test.mp3");
soundToPlay.play();
}
}
class SoundJLayer extends PlaybackListener implements Runnable
{
private String filePath;
private AdvancedPlayer player;
private Thread playerThread;
public SoundJLayer(String filePath)
{
this.filePath = filePath;
}
public void play()
{
try
{
String urlAsString = "file:\\C:\\Users\\Lorenz Kyle\\Desktop\\CODES by Lo\\Case 4\\Server\\Mp3MulticastServer\\src\\test.mp3";
this.player = new AdvancedPlayer
(
new java.net.URL(urlAsString).openStream(),
javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice()
);
this.player.setPlayBackListener(this);
this.playerThread = new Thread(this, "AudioPlayerThread");
this.playerThread.start();
}
catch (Exception ex)
{
}
}
// PlaybackListener members
public void playbackStarted(PlaybackEvent playbackEvent)
{
System.out.println("playbackStarted");
}
public void playbackFinished(PlaybackEvent playbackEvent)
{
System.out.println("playbackEnded");
}
// Runnable members
public void run()
{
try
{
this.player.play();
}
catch (javazoom.jl.decoder.JavaLayerException ex)
{
ex.printStackTrace();
}
}
}