当前位置: 首页>>代码示例>>Java>>正文


Java SourceDataLine.stop方法代码示例

本文整理汇总了Java中javax.sound.sampled.SourceDataLine.stop方法的典型用法代码示例。如果您正苦于以下问题:Java SourceDataLine.stop方法的具体用法?Java SourceDataLine.stop怎么用?Java SourceDataLine.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.sound.sampled.SourceDataLine的用法示例。


在下文中一共展示了SourceDataLine.stop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: playSound

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
/**
 * Play a sound.
 *
 * @param in The {@code AudioInputStream} to play.
 * @return True if the stream was played without incident.
 * @exception IOException if unable to read or write the sound data.
 */
private boolean playSound(AudioInputStream in) throws IOException {
    boolean ret = false;

    SourceDataLine line = openLine(in.getFormat());
    if (line == null) return false;
    try {
        startPlaying();
        int rd;
        while (keepPlaying() && (rd = in.read(data)) > 0) {
            line.write(data, 0, rd);
        }
        ret = true;
    } finally {
        stopPlaying();
        line.drain();
        line.stop();
        line.close();
    }
    return ret;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:28,代码来源:SoundPlayer.java

示例2: playRecorded

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
void playRecorded(AudioFormat format, byte[] data) throws Exception {
    //SourceDataLine line = AudioSystem.getSourceDataLine(format);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);

    line.open();
    line.start();

    int remaining = data.length;
    while (remaining > 0) {
        int avail = line.available();
        if (avail > 0) {
            if (avail > remaining)
                avail = remaining;
            int written = line.write(data, data.length - remaining, avail);
            remaining -= written;
            log("Playing: " + written + " bytes written");
        } else {
            delay(100);
        }
    }

    line.drain();
    line.stop();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:bug6372428.java

示例3: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException,                                                                                                LineUnavailableException
{
  byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat); 
  if (line != null)
  {
    // Start
    line.start();
    int nBytesRead = 0, nBytesWritten = 0;
    while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
        if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
    // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
  } 
}
 
开发者ID:debmalya,项目名称:JavaVAD,代码行数:22,代码来源:MP3Player.java

示例4: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
	byte[] data = new byte[4096];
	SourceDataLine line = getLine(targetFormat);		
	if (line != null)
	{
	  // Start
	  line.start();
	  int nBytesRead = 0, nBytesWritten = 0;
	  while (nBytesRead != -1)
	  {
		nBytesRead = din.read(data, 0, data.length);
		if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
	  }
	  // Stop
	  line.drain();
	  line.stop();
	  line.close();
	  din.close();
	}		
}
 
开发者ID:fredsa,项目名称:forplay,代码行数:22,代码来源:PlayerTest.java

示例5: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
    byte[] data = new byte[4096];
    SourceDataLine line = getLine(targetFormat);        
    if (line != null)
    {
      // Start
      line.start();
      int nBytesRead = 0, nBytesWritten = 0;
      while (nBytesRead != -1)
      {
        nBytesRead = din.read(data, 0, data.length);
        if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
      }
      // Stop
      line.drain();
      line.stop();
      line.close();
      din.close();
    }       
}
 
开发者ID:fredsa,项目名称:forplay,代码行数:22,代码来源:SkipTest.java

示例6: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private static void rawplay(AudioFormat targetFormat, 
                                   AudioInputStream din) throws IOException, LineUnavailableException
{
   byte[] data = new byte[4096];
  SourceDataLine line = getLine(targetFormat);		
  if (line != null)
  {
     // Start
    line.start();
     int nBytesRead = 0, nBytesWritten = 0;
     while (nBytesRead != -1)
    {
        nBytesRead = din.read(data, 0, data.length);
         if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
    }
     // Stop
    line.drain();
    line.stop();
    line.close();
    din.close();
  }		
}
 
开发者ID:tino1b2be,项目名称:DTMF-Decoder,代码行数:23,代码来源:Test.java

示例7: closeAllSoundSources

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void closeAllSoundSources() {
  if (this.queue.isEmpty()) {
    return;
  }

  while (this.queue.peek() != null) {
    final SourceDataLine clip = this.queue.poll();
    clip.stop();
    clip.flush();
    clip.close();
  }
}
 
开发者ID:gurkenlabs,项目名称:litiengine,代码行数:13,代码来源:SoundSource.java

示例8: doMixerSDL

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private static boolean doMixerSDL(Mixer mixer, AudioFormat format) {
    if (mixer==null) return false;
    try {
        System.out.println("Trying mixer "+mixer+":");
            DataLine.Info info = new DataLine.Info(
                                      SourceDataLine.class,
                                      format,
                                      (int) samplerate);

            SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);
        System.out.println("  - got sdl: "+sdl);
        System.out.println("  - open with format "+format);
        sdl.open(format);
        System.out.println("  - start...");
        sdl.start();
        System.out.println("  - write...");
        sdl.write(buffer, 0, buffer.length);
        Thread.sleep(200);
        System.out.println("  - drain...");
        sdl.drain();
        System.out.println("  - stop...");
        sdl.stop();
        System.out.println("  - close...");
        sdl.close();
        System.out.println("  - closed");
    } catch (Throwable t) {
        System.out.println("  - Caught exception. Not failed.");
        System.out.println("  - "+t.toString());
        return false;
    }
    return true;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:ChangingBuffer.java

示例9: stream

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
/**
* Small sections of audio bytes are read off, watching for a call to stop, pause, restart, or mute the audio.
* @param targetFormat Format the audio will be changed to.
* @param din The audio stream.
*/
private void stream(AudioFormat targetFormat, AudioInputStream din)
{
	try {
		byte[] data = new byte[byteChunkSize];
		SourceDataLine line = getLine(targetFormat);
		if (line != null) 
		{
			line.start();
			int nBytesRead = 0;
			while(nBytesRead != -1 && running && !restart && !isStopped) 
			{
				nBytesRead = din.read(data, 0, data.length);
				if(nBytesRead != -1)
				{
					if (mute)
						line.write(muteData, 0, nBytesRead);
					else
						line.write(data, 0, nBytesRead);
				
				}
				while(pause && running)
					wait(15);
			}
			line.drain();
			line.stop();
			line.close();
			din.close();
		}
	} catch(Exception e) {
		System.err.println("Problem playing audio!");
		e.printStackTrace();
	}
}
 
开发者ID:Sw4T,项目名称:GrooveBerry,代码行数:39,代码来源:AudioFile.java

示例10: rawplay

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
private void rawplay(AudioFormat targetFormat, AudioInputStream dataIn) throws IOException, LineUnavailableException {
    // get buffer
    byte[] data = new byte[4096];

    // get line
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat);
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

    if (line != null) {
        // open it
        line.open();

        // start
        line.start();
        int nBytesRead = 0, nBytesWritten = 0;
        while (nBytesRead != -1) {
            nBytesRead = dataIn.read(data, 0, data.length);
            if (nBytesRead != -1) {
                nBytesWritten = line.write(data, 0, nBytesRead);
            }
        }

        // stop line
        line.drain();
        line.stop();
        line.close();

        // stop input stream
        dataIn.close();
    }
}
 
开发者ID:Trilarion,项目名称:java-vorbis-support,代码行数:32,代码来源:SPIDecoderTest.java

示例11: play

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static int play(boolean shouldPlay) {
    int res = 0;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    try {
        println("Getting line from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening line...");
        println("  -- if the program is hanging here, kill the process that has blocks the audio device now.");
        source.open(audioFormat);
        println("Starting line...");
        source.start();
        println("Writing audio data for 1 second...");
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < 1000) {
            writeData();
            Thread.sleep(100);
        }
        res = 1;
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
        }
        return 3;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        if (shouldPlay) {
            println("ERROR: the line should be available now!.");
            println("       Verify that you killed the other audio process.");
        } else {
            println("Correct behavior! the bug is fixed.");
        }
        res = 2;
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Draining...");
        try {
            source.drain();
        } catch (NullPointerException npe) {
            println("(NullPointerException: bug fixed in J2SE 1.4.2");
        }
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        source = null;
    }
    return res;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:54,代码来源:PlaySine.java

示例12: play

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("You should have heard a short tone,");
        println("followed by silence (no repeating tones).");
        key();
        source.write(audioData, 0, audioData.length);
        println("Now you should have heard another short tone.");
        println("If you did not hear a second tone, or more than 2 tones,");
        println("the test is FAILED.");
        println("otherwise, if you heard a total of 2 tones, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:DirectSoundUnderrunSilence.java

示例13: play

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static void play(Mixer mixer) {
    int res = 0;
    try {
        println("Getting SDL from mixer...");
        source = (SourceDataLine) mixer.getLine(info);
        println("Opening SDL...");
        source.open(audioFormat);
        println("Writing data to SDL...");
        source.write(audioData, 0, audioData.length);
        println("Starting SDL...");
        source.start();
        println("Now open your ears:");
        println("- you should have heard a short tone,");
        println("  followed by silence.");
        println("- if after a while you hear repeated tones,");
        println("  the bug is NOT fixed.");
        println("- if the program remains silent after the ");
        println("  initial tone, the bug is fixed.");
        key();
    } catch (IllegalArgumentException iae) {
        println("IllegalArgumentException: "+iae.getMessage());
        println("Sound device cannot handle this audio format.");
        println("ERROR: Test environment not correctly set up.");
        if (source!=null) {
            source.close();
            source = null;
        }
        return;
    } catch (LineUnavailableException lue) {
        println("LineUnavailableException: "+lue.getMessage());
        println("This is normal for some mixers.");
    } catch (Exception e) {
        println("Unexpected Exception: "+e.toString());
    }
    if (source != null) {
        println("Stopping...");
        source.stop();
        println("Closing...");
        source.close();
        println("Closed.");
        source = null;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:DirectSoundRepeatingBuffer.java

示例14: main

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("This test should only be run on Windows.");
    System.out.println("Make sure that the speakers are connected and the volume is up.");
    System.out.println("Close all other programs that may use the soundcard.");

    System.out.println("You'll hear a 2-second tone. when the tone finishes,");
    System.out.println("  there should be no noise. If you hear a short tick/noise,");
    System.out.println("  the bug still applies.");

    System.out.println("Press ENTER to continue.");
    System.in.read();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("1")) WorkAround1 = true;
        if (args[i].equals("2")) WorkAround2 = true;
    }
    if (WorkAround1) System.out.println("Using work around1: appending silence");
    if (WorkAround2) System.out.println("Using work around2: waiting before close");

    int zerolen = 0; // how many 0-bytes will be appended to playback
    if (WorkAround1) zerolen = 1000;
    int seconds = 2;
    int sampleRate = 8000;
    double frequency = 1000.0;
    double RAD = 2.0 * Math.PI;
    AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true);
    System.out.println("Format: "+af);
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);
    SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info);
    System.out.println("Line: "+source);
    if (source.toString().indexOf("MixerSourceLine")>=0) {
        System.out.println("This test only applies to non-Java Sound Audio Engine!");
        return;
    }
    System.out.println("Opening...");
    source.open(af);
    System.out.println("Starting...");
    source.start();
    int datalen = sampleRate * seconds;
    byte[] buf = new byte[datalen+zerolen];
    for (int i=0; i<datalen; i++) {
        buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
    }
    System.out.println("Writing...");
    source.write(buf,0,buf.length);
    System.out.println("Draining...");
    source.drain();
    System.out.println("Stopping...");
    source.stop();
    if (WorkAround2) {
        System.out.println("Waiting 200 millis...");
        Thread.sleep(200);
    }
    System.out.println("Closing...");
    source.close();
    System.out.println("Done.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:58,代码来源:TickAtEndOfPlay.java

示例15: run

import javax.sound.sampled.SourceDataLine; //导入方法依赖的package包/类
@Override
public void run() {
    try {
        final AudioFormat audioformat = new AudioFormat(javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED, skiprate, 16, 1, 2, skiprate, false);
        final javax.sound.sampled.DataLine.Info info = new javax.sound.sampled.DataLine.Info(SourceDataLine.class, audioformat);
        source = (SourceDataLine) AudioSystem.getLine(info);
        source.open(audioformat);
        source.start();
    } catch (final Exception exception) {
        stoped = 1;
    }
    boolean flag = false;
    while (stoped == 0) {
        try {
            final int i = skiprate;
            int j = stream.available();
            if (j % 2 != 0) {
                j++;
            }
            byte[] abyte0 = new byte[j <= i ? j : i];
            final int l = stream.read(abyte0, 0, abyte0.length);
            if (l == -1 || rollBackPos != 0 && j < rollBackTrig) {
                flag = true;
            }
            if (flag) {
                if (l != -1) {
                    source.write(abyte0, 0, abyte0.length);
                }
                stream.reset();
                if (rollBackPos != 0) {
                    stream.skip(rollBackPos);
                }
                int k = stream.available();
                if (k % 2 != 0) {
                    k++;
                }
                abyte0 = new byte[k <= i ? k : i];
                stream.read(abyte0, 0, abyte0.length);
                flag = false;
            }
            source.write(abyte0, 0, abyte0.length);
        } catch (final Exception exception1) {
            System.out.println("Play error: " + exception1);
            stoped = 1;
        }
        try {
            Thread.sleep(200L);
        } catch (final InterruptedException ignored) {
        }
    }
    source.stop();
    source.close();
    source = null;
    stoped = 2;
}
 
开发者ID:uwx,项目名称:OpenNFMM,代码行数:56,代码来源:SuperClip.java


注:本文中的javax.sound.sampled.SourceDataLine.stop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。