Java 8 TargetDataLine - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 TargetDataLine class.

This code example shows how to use the following methods:start, open
	public JavaSoundAudioRecorder (int samplingRate, boolean isMono) {
		try { 
			AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, samplingRate, 16, isMono ? 1 : 2, isMono ? 2 : 4,
				samplingRate, false);
			line = AudioSystem.getTargetDataLine(format);
			line.open(format, buffer.length);
			line.start();
		} catch (Exception ex) {
			throw new GdxRuntimeException("Error creating JavaSoundAudioRecorder.", ex);
		} 
	} 
 
	public void read (short[] samples, int offset, int numSamples) {
		if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2];
 
		int toRead = numSamples * 2;
		int read = 0;
		while (read != toRead)
			read += line.read(buffer, read, toRead - read);
 
6
CodeRank
This code example shows how to use the following methods:start, stop, close, open
		/* Starting the TargetDataLine. It tells the line that 
		   we now want to read data from it. If this method 
		   isn't called, we won't 
		   be able to read data from the line at all. 
		*/ 
		m_line.start();
 
		/* Starting the thread. This call results in the 
		   method 'run()' (see below) being called. There, the 
		   data is actually read from the line. 
		*/ 
		super.start(); 
	} 
 
 
	/** Stops the recording. 
 
	    Note that stopping the thread explicitely is not necessary. Once 
	    no more data can be read from the TargetDataLine, no more data 
	    be read from our AudioInputStream. And if there is no more 

6
CodeRank
This code example shows how to use the following methods:start, stop, close, open
		/* Starting the TargetDataLine. It tells the line that 
		   we now want to read data from it. If this method 
		   isn't called, we won't 
		   be able to read data from the line at all. 
		*/ 
		m_line.start();
 
		/* Starting the thread. This call results in the 
		   method 'run()' (see below) being called. There, the 
		   data is actually read from the line. 
		*/ 
		super.start(); 
	} 
 
 
	/** Stops the recording. 
 
	    Note that stopping the thread explicitely is not necessary. Once 
	    no more data can be read from the TargetDataLine, no more data 
	    be read from our AudioInputStream. And if there is no more 

6
CodeRank
This code example shows how to use the following methods:open
    	DataLine.Info info = new DataLine.Info(TargetDataLine.class,
            audioFormat);
    	TargetDataLine targetDataLine = null;
    	try { 
    		targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
    		targetDataLine.open(audioFormat);
    	} catch (LineUnavailableException e) {
    		System.err.println("unable to get a recording line");
    		e.printStackTrace();
    		//System.exit(1); 
    	} 
 
    	/* 
    	 * Again for simplicity, we've hardcoded the audio file type, too. 
    	 */ 
    	AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
 
        AudioRecorder.BufferingRecorder recorder = new AudioRecorder.BufferingRecorder(targetDataLine, targetType, targetFile, (int) millis);
 
    	/* 

5
CodeRank
This code example shows how to use the following methods:stop, close
     * It's not a good idea to call this method just 'stop()' because stop() is 
     * a (deprecated) method of the class 'Thread'. And we don't want to 
     * override this method. 
     */ 
    public void stopRecording() { 
        m_line.stop();
        m_line.close();
    } 
 
    /** 
     * Main working method. You may be surprised that here, just 
     * 'AudioSystem.write()' is called. But internally, it works like this: 
     * AudioSystem.write() contains a loop that is trying to read from the 
     * passed AudioInputStream. Since we have a special AudioInputStream that 
     * gets its data from a TargetDataLine, reading from the AudioInputStream 
     * leads to reading from the TargetDataLine. The data read this way is then 
     * written to the passed File. Before writing of audio data starts, a header 
     * is written according to the desired audio file type. Reading continues 
     * untill no more data can be read from the AudioInputStream. In our case, 
     * this happens if no more data can be read from the TargetDataLine. This, 
5
CodeRank
This code example shows how to use the following methods:getFormat, open
This code example shows how to use the following methods:stop, close
     * It's not a good idea to call this method just 'stop()' because stop() is 
     * a (deprecated) method of the class 'Thread'. And we don't want to 
     * override this method. 
     */ 
    public void stopRecording() { 
        m_line.stop();
        m_line.close();
    } 
 
    /** 
     * Main working method. You may be surprised that here, just 
     * 'AudioSystem.write()' is called. But internally, it works like this: 
     * AudioSystem.write() contains a loop that is trying to read from the 
     * passed AudioInputStream. Since we have a special AudioInputStream that 
     * gets its data from a TargetDataLine, reading from the AudioInputStream 
     * leads to reading from the TargetDataLine. The data read this way is then 
     * written to the passed File. Before writing of audio data starts, a header 
     * is written according to the desired audio file type. Reading continues 
     * untill no more data can be read from the AudioInputStream. In our case, 
     * this happens if no more data can be read from the TargetDataLine. This, 
5
CodeRank
This code example shows how to use the following methods:flush, close
		} 
	} 
 
	public void close() 
	{ 
		targetLine.flush();
		targetLine.close();
	} 
} 
5
CodeRank
This code example shows how to use the following methods:stop, close
		} 
 
		//stop recording 
		public void stopRecording() 
		{ 
			m_line.stop();
			m_line.close();
		} 
 
		public void run() 
		{ 
			m_line.start();
			try 
			{ 
				AudioSystem.write(
						m_audioInputStream,
						m_targetType,
						m_outputFile);
			} 
			catch (IOException e)
5
CodeRank
This code example shows how to use the following methods:stop, close
		} 
 
		//stop recording 
		public void stopRecording() 
		{ 
			m_line.stop();
			m_line.close();
		} 
 
		public void run() 
		{ 
			m_line.start();
			try 
			{ 
				AudioSystem.write(
						m_audioInputStream,
						m_targetType,
						m_outputFile);
			} 
			catch (IOException e)
5
CodeRank
See Code Examples for Java 8 TargetDataLine Methods: