“Java 播放本地音频”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
在线语音合成:https://www.zaixianai.cn/voiceCompose | 在线语音合成:https://www.zaixianai.cn/voiceCompose | ||
− | + | https://github.com/jihch/public/tree/main/feiyan | |
注意:只支持 wav 音频文件<syntaxhighlight lang="java"> | 注意:只支持 wav 音频文件<syntaxhighlight lang="java"> | ||
import java.applet.Applet; | import java.applet.Applet; |
2022年11月25日 (五) 04:51的版本
在线语音合成:https://www.zaixianai.cn/voiceCompose
https://github.com/jihch/public/tree/main/feiyan
注意:只支持 wav 音频文件
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
public class MusicPlayer {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
String pathname = "I:\\Program Files (x86)\\MediaEditor\\AudioFiles\\音效\\动物世界\\猫.wav";
File f = new File(pathname);
URI uri = f.toURI();
AudioClip audioClip = Applet.newAudioClip(uri.toURL());
audioClip.play();
TimeUnit.SECONDS.sleep(5);
}
}
https://www.codejava.net/coding/how-to-play-back-audio-in-java-with-examples
示例2:同步播放
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class AudioPlayer implements LineListener {
/**
* this flag indicates whether the playback completes or not.
*/
boolean playCompleted;
/**
* Play a given audio file.
*
* @param audioFilePath Path of the audio file.
*/
void play(String audioFilePath) {
File audioFile = new File(audioFilePath);
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
while (!playCompleted) {
// wait for the playback completes
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
audioClip.close();
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
/**
* Listens to the START and STOP events of the audio line.
*/
@Override
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted = true;
System.out.println("Playback completed.");
}
}
}
public class AudioPlayerTest {
public static void main(String[] args) {
String pathname = "E:\\record\\2022\\11\\21\\%s.wav";
for (int i = 1; i <= 10; i++) {
new AudioPlayer().play(String.format(pathname, i + ""));
}
}
}
更好的版本
import java.util.concurrent.TimeUnit;
public class AudioPlayerTest {
public static void main(String[] args) throws InterruptedException {
String pathname = "E:\\record\\2022\\11\\21\\%s.wav";
for (int i = 1; i <= 10; i++) {
int finalI = i;
new Thread(() -> {
new AudioPlayer().play(String.format(pathname, finalI + ""));
}).start();
TimeUnit.SECONDS.sleep(1);
}
}
}