比如
//A.java
import java.applet.AudioClip;
public class A
{
  AudioClip bg_sound;
  A()
  {
    //???创建??
  }
  
  public static void main(String[] args)
  {
    bg_sound.loop();  
  }
}

解决方案 »

  1.   

    import java.io.*;
    import com.sun.media.sound.*;public class FramAudio{
    public FramAudio(){JavaSoundAudioClip player;
    try{
    FileInputStream ff = new FileInputStream("a.wav");
    player= new JavaSoundAudioClip(ff);
    player.play();
    }catch(Exception e) {
    System.out.println("error");
    e.printStackTrace();
    }}
    public static void main(String arg[]){
    FramAudio fa=new FramAudio();
    }
    }
    可以放wav和midau没有试过
      

  2.   

    to febchen() :
    恩。能运行。奇怪了,com.sun.media.sound包和里边的JavaSoundAudioClip类。我怎么找不到API。
    不是J2SE自带的吗?那又如何运行得了?
    我想看看API,找找那个类的其他方法。比如循环播放啊,停止啊什么的。
      

  3.   

    楼主很坚持啊javax.sound.midi.spi和javaw.sound.sampled.spi
    里也有支持的媒体的类,但没用过,不过倒是有帮助.还有就是jmf了,在sun可以下载到.
      

  4.   

    你用这种方法!
    import java.io.File;import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    /**
     * @author Administrator
     *
     * To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Generation - Code and Comments
     */
    public class Audio extends Thread
    {
    public AudioInputStream currentSound;
    public AudioFormat format;

    public Audio() throws Exception
    {
    try{

    File soundFile  = new File("ring.wav");
    currentSound    = AudioSystem.getAudioInputStream(soundFile);
    format          = currentSound.getFormat(); 
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public void play()throws Exception
    {
    try{
    //Sequencer sequencer= MidiSystem.getSequencer();
    //sequencer.open();
    Clip clip = (Clip)currentSound;
    clip.start();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public void loop() throws Exception
    {
    try
    {
    Clip clip = (Clip)currentSound;
    clip.loop(60);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    }
    public static void main(String[] args)
    {
    try{
    Audio ds = new Audio();
    ds.play();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }


    }
    }
      

  5.   

    我在APPLET里放过的
    APPLICATION还没
    但肯定可以的
      

  6.   

    你构造一个这个类的对象,然后再acitonperform的时候调用相应的函数。不就可以了??
      

  7.   

    楼上兄台,你给我的已经很完整了,有类有main ,里也创建了对象。我只把File soundFile  = new File("ring.wav");里的文件改成我实际有的wav。但是有运行异常。
    java.lang.ClassCastException: javax.sound.sampled.AudioInputStream
    俺不懂
      

  8.   

    这样的,clip是一个interface,你可以写一个借口。不过无所谓,你就这么用是可以的
      

  9.   

    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.DataLine;
    import javax.sound.sampled.SourceDataLine;public class TestBase implements Runnable {
      private static final int BUFFER_SIZE = 64000;
      private String fileToPlay = "ring.wav";
      private boolean pause = false;
      private boolean stop = false;
      private static boolean threadExit = false;
      private static boolean stopped = true;
      private static boolean paused = false;
      private static boolean playing = false;
      public static Object synch = new Object();
      private Thread playerThread = null;  public TestBase() {
       }  public void run() {
        while (! threadExit)  {
         waitforSignal();
         if (! stopped)
              playMusic();
         }
      }  public void endThread() {
         threadExit = true;
         synchronized(synch) {
            synch.notifyAll();
            }
         try {
            Thread.sleep(500);
            } catch (Exception ex) {}
      } public void waitforSignal() {
       try {
            synchronized(synch) {
               synch.wait();
                }
            } catch (Exception ex) {}
     }  public void play() {
       if ((!stopped) || (paused)) return;   if (playerThread == null)  {
         playerThread = new Thread(this);
         playerThread.start();
         try {
             Thread.sleep(500);
             } catch (Exception ex) {}
        }    synchronized(synch) {
          stopped = false;
          synch.notifyAll();
         }
      }  public void setFileToPlay(String fname) {
       fileToPlay = fname;
      }  public void playFile(String fname) {    setFileToPlay(fname);
        play();
      }  public void playMusic() {
          byte[] audioData = new byte[BUFFER_SIZE];
          AudioInputStream ais = null;
          SourceDataLine  line = null;
          AudioFormat baseFormat = null;
          try  {
    ais = AudioSystem.getAudioInputStream(new File (fileToPlay));
          }
          catch (Exception e) {
                }      if (ais != null) {
          baseFormat = ais.getFormat();
          line = getLine(baseFormat);
               if (line == null) {
               AudioFormat decodedFormat = new AudioFormat(
    AudioFormat.Encoding.PCM_SIGNED,
    baseFormat.getSampleRate(),
    16,
    baseFormat.getChannels(),
    baseFormat.getChannels() * 2,
    baseFormat.getSampleRate(),
    false);
                ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
                line = getLine(decodedFormat);
             }
           }
           if (line == null) return;  // 不能播放此文件       playing = true;
           line.start();
           int inBytes = 0;
           while ((inBytes != -1) && (!stopped) && (!threadExit))  {
    try  {
    inBytes = ais.read(audioData, 0, BUFFER_SIZE);
    }
    catch (IOException e)  {
    e.printStackTrace();
    } if (inBytes >= 0) {
    int outBytes = line.write(audioData, 0, inBytes);
    }                         if (paused)
                                 waitforSignal();
               }
            line.drain();
            line.stop();
          line.close();
          playing = false;
         }  public void stop() {
        if(paused) return;
           stopped = true;
        waitForPlayToStop();
      }  public void waitForPlayToStop() {
         while( playing)
         try {
           Thread.sleep(500);
           } catch (Exception ex) {}
      }  public void pause() {
        if (stopped) return;
        synchronized(synch) {
           paused = !paused;
           synch.notifyAll();
          }
      }  private SourceDataLine getLine(AudioFormat audioFormat)  {
         SourceDataLine res = null;
       DataLine.Info info = new DataLine.Info(
    SourceDataLine.class,
    audioFormat);
    try  {
    res = (SourceDataLine) AudioSystem.getLine(info);
    res.open(audioFormat);
    }
    catch (Exception e) { }
          return res;
      }
      public static void main(String[] args)
      {
       try{
       System.out.println("test");
          TestBase ds = new TestBase();
         
         
         
       }catch(Exception e)
    {
       System.out.println(e);
       }
      
      
      }
      }
      

  10.   

    package sound;
    import sun.audio.*;
    import java.io.*;public class TestSound 
    {
    public TestSound()
    {
    } public void openSound()
    {
     try {
      FileInputStream fileau=new FileInputStream("1.wav");
      AudioStream as=new AudioStream(fileau);
      AudioPlayer.player.start(as);
     }
     catch (Exception e) {}
     
    }
     public static void main(String args[])
     {
    TestSound t=new TestSound();
    t.openSound();
    }
    }
    把1.wav放在sound目录的上一个目录中。就可以