查看“Java 播放本地音频”的源代码
←
Java 播放本地音频
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
在线语音合成:https://www.zaixianai.cn/voiceCompose https://translate.google.com/translate_tts?ie=UTF-&&client=tw-ob&tl=zh&q=%E5%BC%80%E5%A7%8B https://github.com/jihch/public/tree/main/feiyan 注意:只支持 wav 音频文件<syntaxhighlight lang="java"> 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); } } </syntaxhighlight> https://www.codejava.net/coding/how-to-play-back-audio-in-java-with-examples === 示例2:同步播放 === <syntaxhighlight lang="java"> 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."); } } } </syntaxhighlight><syntaxhighlight lang="java"> 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 + "")); } } } </syntaxhighlight> === 更好的版本 === <syntaxhighlight lang="java"> 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); } } } </syntaxhighlight>
返回至
Java 播放本地音频
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息