⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 player.java

📁 一个JAVA的音频控制的例程
💻 JAVA
字号:
import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class Player implements Runnable {

	SourceDataLine line;

	Thread thread;

	String errStr = "";

	private File file;

	private String fileName;

	private double duration, seconds;

	private AudioInputStream audioInputStream;

	final int bufSize = 16384;

	public Player(AudioInputStream input){
		this.audioInputStream= input;
		
	}
	public void start() {
		errStr = null;
		thread = new Thread(this);
		thread.setName("Play");
		thread.start();
	}

	public void stop() {
		thread = null;
	}

	private void shutDown(String message) {
		if ((errStr = message) != null) {
			System.err.println(errStr);
		}
		if (thread != null) {
			thread = null;
		}
	}

	public void run() {

		// reload the file if loaded by file
		if (file != null) {
			createAudioInputStream(file, false);
		}

		// make sure we have something to play
		if (audioInputStream == null) {
			shutDown("No loaded audio to play back");
			return;
		}
		// reset to the beginnning of the stream
		try {
			audioInputStream.reset();
		} catch (Exception e) {
			shutDown("Unable to reset the stream\n" + e);
			return;
		}

		// get an AudioInputStream of the desired format for playback
		
		AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
				44100, 16, 1, (16 / 8) * 1, 44100, true);
		AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(
				format, audioInputStream);

		if (playbackInputStream == null) {
			shutDown("Unable to convert stream of format " + audioInputStream
					+ " to format " + format);
			return;
		}

		// define the required attributes for our line,
		// and make sure a compatible line is supported.

		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		if (!AudioSystem.isLineSupported(info)) {
			shutDown("Line matching " + info + " not supported.");
			return;
		}

		// get and open the source data line for playback.

		try {
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.open(format, bufSize);
		} catch (LineUnavailableException ex) {
			shutDown("Unable to open the line: " + ex);
			return;
		}

		// play back the captured audio data

		int frameSizeInBytes = format.getFrameSize();
		int bufferLengthInFrames = line.getBufferSize() / 8;
		int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
		byte[] data = new byte[bufferLengthInBytes];
		int numBytesRead = 0;

		// start the source data line
		line.start();

		while (thread != null) {
			try {
				if ((numBytesRead = playbackInputStream.read(data)) == -1) {
					break;
				}
				int numBytesRemaining = numBytesRead;
				while (numBytesRemaining > 0) {
					numBytesRemaining -= line.write(data, 0, numBytesRemaining);
				}
			} catch (Exception e) {
				shutDown("Error during playback: " + e);
				break;
			}
		}
		// we reached the end of the stream. let the data play out, then
		// stop and close the line.
		if (thread != null) {
			line.drain();
		}
		line.stop();
		line.close();
		line = null;
		shutDown(null);
	}

	public void createAudioInputStream(File file, boolean updateComponents) {
		if (file != null && file.isFile()) {
			try {
				this.file = file;
				errStr = null;
				audioInputStream = AudioSystem.getAudioInputStream(file);
				fileName = file.getName();
				long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / audioInputStream
						.getFormat().getFrameRate());
				duration = milliseconds / 1000.0;
			} catch (Exception ex) {
				reportStatus(ex.toString());
			}
		} else {
			reportStatus("Audio file required.");
		}
	}

	private void reportStatus(String msg) {
		if ((errStr = msg) != null) {
		}
		System.out.println(errStr);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -