繁体   English   中英

如何使用Java播放声音

[英]how to play sound using java

我正在尝试在Java中的按钮ckick上播放声音(a.mp3)。

我尝试了这段代码

AudioPlayer.player.start(new FileInputStream(new File(“ E://a.mp3”))));

但是声音不清楚。

我该怎么办(我是Java的初学者)。

   **For Playing sound in java, please refer to the following code.**

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

    // To play sound using Clip, the process need to be alive.
    // Hence, we use a Swing application.
    public class SoundClipTest extends JFrame {

       // Constructor
       public SoundClipTest() {
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("Test Sound Clip");
          this.setSize(300, 200);
          this.setVisible(true);

          try {
             // Open an audio input stream.
             URL url = this.getClass().getClassLoader().getResource("filenamme.mp3");
             AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
             // Get a sound clip resource.
             Clip clip = AudioSystem.getClip();
             // Open audio clip and load samples from the audio input stream.
             clip.open(audioIn);
             clip.start();
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } catch (LineUnavailableException e) {
             e.printStackTrace();
          }
       }

       public static void main(String[] args) {
          new SoundClipTest();
       }
    }

尝试看看这篇文章。 http://www.morgenstille.at/blog/how-to-play-a-mp3-file-in-java-simple-and-beautiful/

JFugue也可以加载和播放Midi文件。

看看JLayer 这是几年前我能找到的最好的图书馆。 它不是完美的,但应该满足您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM