Playing Streaming Sampled Audio

try { // From file AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile")); // From URL stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile")); // At present, ALAW and ULAW encodings must be converted // to PCM_SIGNED before it can be played AudioFormat format = stream.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits()*2, format.getChannels(), format.getFrameSize()*2, format.getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(format, stream); } // Create line SourceDataLine.Info info = new DataLine.Info( SourceDataLine.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize())); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(stream.getFormat()); line.start(); // Continuously read and play chunks of audio int numRead = 0; byte[] buf = new byte[line.getBufferSize()]; while ((numRead = stream.read(buf, 0, buf.length)) >= 0) { int offset = 0; while (offset < numRead) { offset += line.write(buf, offset, numRead-offset); } } line.drain(); line.stop(); } catch (MalformedURLException e) { } catch (IOException e) { } catch (LineUnavailableException e) { } catch (UnsupportedAudioFileException e) { }

Comments

9 Feb 2010 - 11:59pm by Anonymous (not verified)

How can i run the jmf program in the command prompt?i was ran the program like by this
javac SimplePlayerApplet.java
appletviewer SimplePlayerApplet.html
but it displays some error like this
java.lang.NoClassDefFoundError: javax/media/ControllerListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:192)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:127)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:632)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:786)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:715)
at sun.applet.AppletPanel.run(AppletPanel.java:369)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: javax.media.ControllerListener
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:194)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:127)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 12 more
please any one tell me the reason for that.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.