gpt4 book ai didi

java - .wav 文件可以播放,但 .aiff 文件不能播放

转载 作者:太空宇宙 更新时间:2023-11-04 08:03:10 32 4
gpt4 key购买 nike

作为我上一个问题(仍未解决)的后续问题,GUI event not triggering consistently ,我发现了另一个怪癖。下面的代码创建并播放 .wav 文件:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class audioTest extends JFrame {

private static final long serialVersionUID = 1L;
TargetDataLine targetDataLine;
AudioCapture audCap = new AudioCapture();

public static void main(String[] args) {
new audioTest();
}

public audioTest() {

layoutTransporButtons();
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 100);
setVisible(true);
}

public void layoutTransporButtons() {

final JPanel guiButtonPanel = new JPanel();
final JButton captureBtn = new JButton("Record");
final JButton stopBtn = new JButton("Stop");
final JButton playBtn = new JButton("Playback");
guiButtonPanel.setLayout(new GridLayout());
this.add(guiButtonPanel);
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);

JRadioButton[] radioBtnArray;
AudioFileFormat.Type[] fileTypes;

// Register anonymous listeners
captureBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
// Capture input data from the microphone
audCap.captureAudio();
}
});
guiButtonPanel.add(captureBtn);

stopBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
audCap.stopRecordAndPlayback = true;
audCap.stopRecording();
}
});
guiButtonPanel.add(stopBtn);

playBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopBtn.setEnabled(true);
audCap.playAudio();
}
});
guiButtonPanel.add(playBtn);
}

class AudioCapture {

volatile boolean stopRecordAndPlayback = false;
AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
private String wavName;
private File audioFile;

/**
* capture audio input from microphone and save as .wav file
*/
public void captureAudio() {

wavName = JOptionPane.showInputDialog(null,
"enter name of file to be recorded:");
try {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
// Select an available mixer
Mixer mixer = AudioSystem.getMixer(mixerInfo[1]);
// Get everything set up for capture
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
// Get a TargetDataLine on the selected mixer.
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
// Prepare the line for use.
targetDataLine.open(audioFormat);
targetDataLine.start();
// Create a thread to capture the microphone
Thread captureThread = new CaptureThread();
captureThread.start();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}

/**
* This method plays back the audio data that has
* been chosen by the user
*/
public void playAudio() {
// add file chooser
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(audioFile);
int returnVal = chooser.showOpenDialog(chooser);
// retrieve chosen file
if (returnVal == JFileChooser.APPROVE_OPTION) {
// create the file
audioFile = chooser.getSelectedFile();
}
// play chosen file
try {
audioInputStream = AudioSystem.getAudioInputStream(audioFile);
audioFormat = audioInputStream.getFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem
.getLine(dataLineInfo);
// Create a thread to play back the data
new PlayThread().start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}

/**
* This method creates and returns an AudioFormat object
*/
private AudioFormat getAudioFormat() {
float sampleRate = 44100.0F;
// 8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
// 8,16
int channels = 1;
// 1,2
boolean signed = true;
// true,false
boolean bigEndian = false;
// true,false
return new AudioFormat(sampleRate, sampleSizeInBits, channels,
signed, bigEndian);
}

/**
* Inner class to capture data from microphone
*/
class CaptureThread extends Thread {
// An arbitrary-size temporary holding buffer
byte tempBuffer[] = new byte[10000];

public void run() {
// reset stopCapture to false
stopRecordAndPlayback = false;
// record as wave
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
// take user input file name and append file type
audioFile = new File(wavName + ".wav");

try {
targetDataLine.open(audioFormat);
targetDataLine.start();
while (!stopRecordAndPlayback) {
AudioSystem.write(new AudioInputStream(targetDataLine),
fileType, audioFile);
}
targetDataLine.stop();
targetDataLine.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* method to stop capture
*/
public void stopRecording() {
// targetDataLine.stop();
// targetDataLine.close();
// System.out.println("stopped");
}

/**
* Inner class to play back the data
*/
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];

public void run() {
// reset stop button
stopRecordAndPlayback = false;

try {
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1
&& stopRecordAndPlayback == false) {
if (cnt > 0) {
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
}

我尝试更改捕获部分以记录 .aiff 文件,该文件有效,但播放现在是静音的。我可以找到该文件并通过其他方式播放,它工作正常,但在这个程序中不行。

我为记录 .aiff 而更改的行是:

// record as aiff
AudioFileFormat.Type fileType = AudioFileFormat.Type.AIFF;
// take user input file name and append file type
audioFile = new File(wavName + ".AIFF");

有人知道为什么 .wav 文件可以通过此代码播放,而 .aiff 文件却不能?

-编辑-我还尝试使用 .aif 作为后缀,但这也不起作用。我想到这可能与存储为 AIFF-C 音频的文件有关,但我找不到任何进一步的信息。

最佳答案

AIFF-C is a compressed audio format ,因此您不应该将其“按原样”发送到音频设备。您需要先将其解压为PCM。

关于java - .wav 文件可以播放,但 .aiff 文件不能播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12517685/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com