2

我找到了在 java 中播放 mp3 的代码,我想使用 UDP 在网络中流式传输它,所以我想将 mp3 文件转换为字节,这样我就可以使用DatagramPacket. 我应该转换什么来做到这一点?

我需要将 60Kbyte 的缓冲区发送到datagrampacket.

            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();
                }

                }
            }
4

0 回答 0