<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_by_API%2Fjavax.sound.sampled%2FLineUnavailableException</id>
		<title>Java by API/javax.sound.sampled/LineUnavailableException - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_by_API%2Fjavax.sound.sampled%2FLineUnavailableException"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_by_API/javax.sound.sampled/LineUnavailableException&amp;action=history"/>
		<updated>2026-04-19T15:31:52Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_by_API/javax.sound.sampled/LineUnavailableException&amp;diff=2123&amp;oldid=prev</id>
		<title> в 17:43, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_by_API/javax.sound.sampled/LineUnavailableException&amp;diff=2123&amp;oldid=prev"/>
				<updated>2010-05-31T17:43:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:43, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_by_API/javax.sound.sampled/LineUnavailableException&amp;diff=2124&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_by_API/javax.sound.sampled/LineUnavailableException&amp;diff=2124&amp;oldid=prev"/>
				<updated>2010-05-31T14:41:49Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== javax.sound.sampled.LineUnavailableException ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import javax.sound.midi.InvalidMidiDataException;&lt;br /&gt;
import javax.sound.midi.MetaEventListener;&lt;br /&gt;
import javax.sound.midi.MetaMessage;&lt;br /&gt;
import javax.sound.midi.MidiSystem;&lt;br /&gt;
import javax.sound.midi.MidiUnavailableException;&lt;br /&gt;
import javax.sound.midi.Sequencer;&lt;br /&gt;
import javax.sound.midi.Synthesizer;&lt;br /&gt;
import javax.sound.sampled.AudioFormat;&lt;br /&gt;
import javax.sound.sampled.AudioInputStream;&lt;br /&gt;
import javax.sound.sampled.AudioSystem;&lt;br /&gt;
import javax.sound.sampled.DataLine;&lt;br /&gt;
import javax.sound.sampled.LineUnavailableException;&lt;br /&gt;
import javax.sound.sampled.SourceDataLine;&lt;br /&gt;
import javax.sound.sampled.UnsupportedAudioFileException;&lt;br /&gt;
/**&lt;br /&gt;
 * This class plays sounds streaming from a URL: it does not have to preload the&lt;br /&gt;
 * entire sound into memory before playing it. It is a command-line application&lt;br /&gt;
 * with no gui. It includes code to convert ULAW and ALAW audio formats to PCM&lt;br /&gt;
 * so they can be played. Use the -m command-line option before MIDI files.&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  // Create a URL from the command-line argument and pass it to the&lt;br /&gt;
  // right static method depending on the presence of the -m (MIDI) option.&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    if (args[0].equals(&amp;quot;-m&amp;quot;))&lt;br /&gt;
      streamMidiSequence(new URL(args[1]));&lt;br /&gt;
    else&lt;br /&gt;
      streamSampledAudio(new URL(args[0]));&lt;br /&gt;
    // Exit explicitly.&lt;br /&gt;
    // This is needed because the audio system starts background threads.&lt;br /&gt;
    System.exit(0);&lt;br /&gt;
  }&lt;br /&gt;
  /** Read sampled audio data from the specified URL and play it */&lt;br /&gt;
  public static void streamSampledAudio(URL url) throws IOException, UnsupportedAudioFileException,&lt;br /&gt;
      LineUnavailableException {&lt;br /&gt;
    AudioInputStream ain = null; // We read audio data from here&lt;br /&gt;
    SourceDataLine line = null; // And write it here.&lt;br /&gt;
    try {&lt;br /&gt;
      // Get an audio input stream from the URL&lt;br /&gt;
      ain = AudioSystem.getAudioInputStream(url);&lt;br /&gt;
      // Get information about the format of the stream&lt;br /&gt;
      AudioFormat format = ain.getFormat();&lt;br /&gt;
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);&lt;br /&gt;
      // If the format is not supported directly (i.e. if it is not PCM&lt;br /&gt;
      // encoded, then try to transcode it to PCM.&lt;br /&gt;
      if (!AudioSystem.isLineSupported(info)) {&lt;br /&gt;
        // This is the PCM format we want to transcode to.&lt;br /&gt;
        // The parameters here are audio format details that you&lt;br /&gt;
        // shouldn&amp;quot;t need to understand for casual use.&lt;br /&gt;
        AudioFormat pcm = new AudioFormat(format.getSampleRate(), 16, format.getChannels(), true,&lt;br /&gt;
            false);&lt;br /&gt;
        // Get a wrapper stream around the input stream that does the&lt;br /&gt;
        // transcoding for us.&lt;br /&gt;
        ain = AudioSystem.getAudioInputStream(pcm, ain);&lt;br /&gt;
        // Update the format and info variables for the transcoded data&lt;br /&gt;
        format = ain.getFormat();&lt;br /&gt;
        info = new DataLine.Info(SourceDataLine.class, format);&lt;br /&gt;
      }&lt;br /&gt;
      // Open the line through which we&amp;quot;ll play the streaming audio.&lt;br /&gt;
      line = (SourceDataLine) AudioSystem.getLine(info);&lt;br /&gt;
      line.open(format);&lt;br /&gt;
      // Allocate a buffer for reading from the input stream and writing&lt;br /&gt;
      // to the line. Make it large enough to hold 4k audio frames.&lt;br /&gt;
      // Note that the SourceDataLine also has its own internal buffer.&lt;br /&gt;
      int framesize = format.getFrameSize();&lt;br /&gt;
      byte[] buffer = new byte[4 * 1024 * framesize]; // the buffer&lt;br /&gt;
      int numbytes = 0; // how many bytes&lt;br /&gt;
      // We haven&amp;quot;t started the line yet.&lt;br /&gt;
      boolean started = false;&lt;br /&gt;
      for (;;) { // We&amp;quot;ll exit the loop when we reach the end of stream&lt;br /&gt;
        // First, read some bytes from the input stream.&lt;br /&gt;
        int bytesread = ain.read(buffer, numbytes, buffer.length - numbytes);&lt;br /&gt;
        // If there were no more bytes to read, we&amp;quot;re done.&lt;br /&gt;
        if (bytesread == -1)&lt;br /&gt;
          break;&lt;br /&gt;
        numbytes += bytesread;&lt;br /&gt;
        // Now that we&amp;quot;ve got some audio data, to write to the line,&lt;br /&gt;
        // start the line, so it will play that data as we write it.&lt;br /&gt;
        if (!started) {&lt;br /&gt;
          line.start();&lt;br /&gt;
          started = true;&lt;br /&gt;
        }&lt;br /&gt;
        // We must write bytes to the line in an integer multiple of&lt;br /&gt;
        // the framesize. So figure out how many bytes we&amp;quot;ll write.&lt;br /&gt;
        int bytestowrite = (numbytes / framesize) * framesize;&lt;br /&gt;
        // Now write the bytes. The line will buffer them and play&lt;br /&gt;
        // them. This call will block until all bytes are written.&lt;br /&gt;
        line.write(buffer, 0, bytestowrite);&lt;br /&gt;
        // If we didn&amp;quot;t have an integer multiple of the frame size,&lt;br /&gt;
        // then copy the remaining bytes to the start of the buffer.&lt;br /&gt;
        int remaining = numbytes - bytestowrite;&lt;br /&gt;
        if (remaining &amp;gt; 0)&lt;br /&gt;
          System.arraycopy(buffer, bytestowrite, buffer, 0, remaining);&lt;br /&gt;
        numbytes = remaining;&lt;br /&gt;
      }&lt;br /&gt;
      // Now block until all buffered sound finishes playing.&lt;br /&gt;
      line.drain();&lt;br /&gt;
    } finally { // Always relinquish the resources we use&lt;br /&gt;
      if (line != null)&lt;br /&gt;
        line.close();&lt;br /&gt;
      if (ain != null)&lt;br /&gt;
        ain.close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // A MIDI protocol constant that isn&amp;quot;t defined by javax.sound.midi&lt;br /&gt;
  public static final int END_OF_TRACK = 47;&lt;br /&gt;
  /* MIDI or RMF data from the specified URL and play it */&lt;br /&gt;
  public static void streamMidiSequence(URL url) throws IOException, InvalidMidiDataException,&lt;br /&gt;
      MidiUnavailableException {&lt;br /&gt;
    Sequencer sequencer = null; // Converts a Sequence to MIDI events&lt;br /&gt;
    Synthesizer synthesizer = null; // Plays notes in response to MIDI events&lt;br /&gt;
    try {&lt;br /&gt;
      // Create, open, and connect a Sequencer and Synthesizer&lt;br /&gt;
      // They are closed in the finally block at the end of this method.&lt;br /&gt;
      sequencer = MidiSystem.getSequencer();&lt;br /&gt;
      sequencer.open();&lt;br /&gt;
      synthesizer = MidiSystem.getSynthesizer();&lt;br /&gt;
      synthesizer.open();&lt;br /&gt;
      sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());&lt;br /&gt;
      // Specify the InputStream to stream the sequence from&lt;br /&gt;
      sequencer.setSequence(url.openStream());&lt;br /&gt;
      // This is an arbitrary object used with wait and notify to&lt;br /&gt;
      // prevent the method from returning before the music finishes&lt;br /&gt;
      final Object lock = new Object();&lt;br /&gt;
      // Register a listener to make the method exit when the stream is&lt;br /&gt;
      // done. See Object.wait() and Object.notify()&lt;br /&gt;
      sequencer.addMetaEventListener(new MetaEventListener() {&lt;br /&gt;
        public void meta(MetaMessage e) {&lt;br /&gt;
          if (e.getType() == END_OF_TRACK) {&lt;br /&gt;
            synchronized (lock) {&lt;br /&gt;
              lock.notify();&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      });&lt;br /&gt;
      // Start playing the music&lt;br /&gt;
      sequencer.start();&lt;br /&gt;
      // Now block until the listener above notifies us that we&amp;quot;re done.&lt;br /&gt;
      synchronized (lock) {&lt;br /&gt;
        while (sequencer.isRunning()) {&lt;br /&gt;
          try {&lt;br /&gt;
            lock.wait();&lt;br /&gt;
          } catch (InterruptedException e) {&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    } finally {&lt;br /&gt;
      // Always relinquish the sequencer, so others can use it.&lt;br /&gt;
      if (sequencer != null)&lt;br /&gt;
        sequencer.close();&lt;br /&gt;
      if (synthesizer != null)&lt;br /&gt;
        synthesizer.close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>