자바 로 녹음 해서 틀 어 주세요.

소개
자바 로 어떻게 녹음 과 소 리 를 재생 하 는 효 과 를 실현 해 야 합 니까?
녹음
[codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.talk.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.SocketException;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

/**
 *         
 * @author suren
 * @date 2015 8 23    8:58:34
 */
public class RecordUtil
{
	private boolean done = false;
	private ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
	private TargetDataLine	dataLine;
	
	public void start()
	{
		System.out.println("start record");
		
		try
		{
			init();
			
			int len = 1024;
			byte[] buf = new byte[len];
			while(!done && (len = dataLine.read(buf, 0, 1024)) != -1)
			{
				bufStream.write(buf, 0, len);
			}
		}
		catch (LineUnavailableException e)
		{
			e.printStackTrace();
		}
		catch (SocketException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * @throws LineUnavailableException 
	 * @throws SocketException 
	 * 
	 */
	public void init() throws LineUnavailableException, SocketException
	{
		if(dataLine == null)
		{
			dataLine = AudioSystem.getTargetDataLine(null);
			
			dataLine.open();
			dataLine.start();
		}
		
		done = false;
	}
	
	public byte[] getData()
	{
		byte[] data = bufStream.toByteArray();
		bufStream.reset();
		
		return data;
	}

	public void finish() throws IOException
	{
		done = true;
		
		if(getData() == null)
		{
			return;
		}
	}
	
	public void close()
	{
		if(dataLine != null && dataLine.isOpen())
		{
			dataLine.close();
		}
	}
}

[/codesyntax]
위의 코드 는 녹음 도구 류 이다.
재생
[codesyntax lang="java"]
/**
 * Copyright By suren.
 * You can get more information from my website:
 * http://surenpi.com
 */
package org.suren.talk.util;

import java.io.ByteArrayOutputStream;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
 *           
 * @author suren
 * @date 2015 8 23    7:04:22
 */
public class PlayerUtil
{
	private SourceDataLine dataLine;
	
	public void init() throws LineUnavailableException
	{
		dataLine = AudioSystem.getSourceDataLine(null);
		dataLine.open();
		dataLine.start();
	}
	
	public void play(ByteArrayOutputStream byteStream)
	{
		if(byteStream == null)
		{
			System.err.println("byteStream is null");
			return;
		}
		
		byte[] data = byteStream.toByteArray();
		byteStream.reset();
		
		play(data);
	}
	
	public void play(byte[] data)
	{
		if(data == null)
		{
			return;
		}
		
		play(data, 0, data.length);
	}
	
	public void play(byte[] data, int off, int length)
	{
		if(dataLine.isOpen())
		{
			dataLine.write(data, off, length);
		}
	}
	
	public void stop()
	{
		dataLine.close();
	}
}

[/codesyntax]원본 보기:http://surenpi.com/2015/08/24/%e7%94%a8java%e5%bd%95%e9%9f%b3%e5%b9%b6%e6%92%ad%e6%94%be/

좋은 웹페이지 즐겨찾기