麻烦在Java中播放wav

编程入门 行业动态 更新时间:2024-10-23 19:20:12
本文介绍了麻烦在Java中播放wav的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试播放

PCM_UNSIGNED 11025.0 Hz, 8 bit, mono, 1 bytes/frame

文件描述 here(1)和这里(2)。

第一种方法有效,但我不想依赖 sun。* 东西。第二个导致只播放一些领先的帧,听起来更像是一个点击。因为我正在玩ByteArrayInputStream,所以不能成为IO问题。

The first approach works, but I don't want to depend on sun.* stuff. The second results in just some leading frames being played, that sounds more like a click. Can't be an IO issue as I'm playing from a ByteArrayInputStream.

Plz分享你为何会发生这种情况的想法。 TIA。

Plz share your ideas on why might this happen. TIA.

推荐答案

我不确定为什么你链接的第二种方法会启动另一个线程;我相信无论如何音频都将在自己的线程中播放。在剪辑完成播放之前,您的应用程序是否完成了问题?

I'm not sure why the second approach you linked to starts another thread; I believe the audio will be played in its own thread anyway. Is the problem that your application finishes before the clip has finished playing?

import javax.sound.sampled.*; import java.io.File; import java.io.IOException; import javax.sound.sampled.LineEvent.Type; private static void playClip(File clipFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException { class AudioListener implements LineListener { private boolean done = false; @Override public synchronized void update(LineEvent event) { Type eventType = event.getType(); if (eventType == Type.STOP || eventType == Type.CLOSE) { done = true; notifyAll(); } } public synchronized void waitUntilDone() throws InterruptedException { while (!done) { wait(); } } } AudioListener listener = new AudioListener(); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(clipFile); try { Clip clip = AudioSystem.getClip(); clip.addLineListener(listener); clip.open(audioInputStream); try { clip.start(); listener.waitUntilDone(); } finally { clip.close(); } } finally { audioInputStream.close(); } }

更多推荐

麻烦在Java中播放wav

本文发布于:2023-07-11 12:14:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094020.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麻烦   Java   wav

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!