java播放声音_如何在Java中播放声音?

这实际上是一个多线程问题.

问题是您开始播放剪辑,但随后却终止了程序,却没有机会播放其结尾. Clip.start()方法不是阻塞操作,这意味着它不会等待,而是启动一个新的守护程序线程来播放声音,该守护程序线程在程序退出main方法后即被杀死.

这是我的other answer的代码示例,用于使用Clip播放音频文件.注意我计算声音持续时间的方式,然后使用sleep()播放它.

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.Clip;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

public class PlaySound {

private static boolean tryToInterruptSound = false;

private static long mainTimeOut = 3000;

private static long startTime = System.currentTimeMillis();

public static synchronized Thread playSound(final File file) {

Thread soundThread = new Thread() {

@Override

public void run() {

try{

Clip clip = null;

AudioInputStream inputStream = null;

clip = AudioSystem.getClip();

inputStream = AudioSystem.getAudioInputStream(file);

AudioFormat format = inputStream.getFormat();

long audioFileLength = file.length();

int frameSize = format.getFrameSize();

float frameRate = format.getFrameRate();

long durationInMiliSeconds =

(long) (((float)audioFileLength / (frameSize * frameRate)) * 1000);

clip.open(inputStream);

clip.start();

System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound started playing!");

Thread.sleep(durationInMiliSeconds);

while (true) {

if (!clip.isActive()) {

System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound got to it's end!");

break;

}

long fPos = (long)(clip.getMicrosecondPosition() / 1000);

long left = durationInMiliSeconds - fPos;

System.out.println("" + (System.currentTimeMillis() - startTime) + ": time left: " + left);

if (left > 0) Thread.sleep(left);

}

clip.stop();

System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound stoped");

clip.close();

inputStream.close();

} catch (LineUnavailableException e) {

e.printStackTrace();

} catch (UnsupportedAudioFileException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound interrupted while playing.");

}

}

};

soundThread.setDaemon(true);

soundThread.start();

return soundThread;

}

public static void main(String[] args) {

Thread soundThread = playSound(new File("C:\Booboo.wav"));

System.out.println("" + (System.currentTimeMillis() - startTime) + ": playSound returned, keep running the code");

try {

Thread.sleep(mainTimeOut );

} catch (InterruptedException e) {

e.printStackTrace();

}

if (tryToInterruptSound) {

try {

soundThread.interrupt();

Thread.sleep(1);

// Sleep in order to let the interruption handling end before

// exiting the program (else the interruption could be handled

// after the main thread ends!).

} catch (Exception e) {

e.printStackTrace();

}

}

System.out.println("" + (System.currentTimeMillis() - startTime) + ": End of main thread; exiting program " +

(soundThread.isAlive() ? "killing the sound deamon thread" : ""));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值