Java的音频 - 如何比赛中播放歌曲(应用程序,而不是Applet的) [英] Java Audio - How to play song during game (Application, not Applet)

查看:189
本文介绍了Java的音频 - 如何比赛中播放歌曲(应用程序,而不是Applet的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我能得到code与URL(这是从下面的反应之一,code)的工作。但我的歌是在wav文件。当我尝试做文件的URL =新的文件(---);,这是行不通的。

1) I was able to get the code working with a URL (it's the code from one of the responses below). But my song is in a wav file. When I try to do File url = new File("---");, it doesn't work.

相反,在堆栈跟踪(感谢您的小费!),它说:

Instead, in the stack trace (thanks for that tip!), it says

无法分配剪辑数据:请求缓冲区太大

我想播放的歌曲是电子乐,约3分钟长。

The song I'm trying to play is techno, about 3 minutes long.

我如何解决剪辑数据的大小问题?

推荐答案

看的 Java的API声音的采样声音。特别剪辑接口和AudioSystem类。

Look at the classes of the Java Sound API for sampled sound. Particularly the Clip interface and the AudioSystem class.

Java的声音使用SPI添加额外的格式内置在J2SE默认的支持。您可以添加基于JMF向mp3plugin.jar提供MP3音乐到JavaSound支持。



对于在循环播放WAV文件,看到这个小例子..

Java Sound uses the SPI to add support for extra formats to the defaults built in to the J2SE. You can add the JMF based mp3plugin.jar to provide support for MP3s to JavaSound.


For playing WAVs in a loop, see this small example..

import java.net.URL;
import javax.sound.sampled.*;

public class LoopSound {

  public static void main(String[] args) throws Exception {
    URL url = new URL(
      "http://pscode.org/media/leftright.wav");
    Clip clip = AudioSystem.getClip();
    AudioInputStream ais = AudioSystem.
      getAudioInputStream( url );
    clip.open(ais);
    clip.loop(5);
    javax.swing.JOptionPane.showMessageDialog(null, "Close to exit!");
  }
} 

这篇关于Java的音频 - 如何比赛中播放歌曲(应用程序,而不是Applet的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆