AudioPlayer可以播放一次文件,但不能循环播放

时间:2011-08-09 23:05:25

标签: java audio

Windows 7 使用NetBeans

所以我使用这个页面编写了这个非常酷的声音引擎 http://www.docjar.org/docs/api/sun/audio/AudioPlayer.html 音频播放器...... 现在我做了循环和一切 一切正常:D 但是当我播放2000赫兹或更低的声音时,一切正常 但是像任何高于3000-4000的那样它会给我这个错误

    10/08/2011 01:53:37 org.game.engine.SoundEngine loop
    SEVERE: null
    java.io.IOException: could not create AudioData object
at sun.audio.AudioStream.getData(AudioStream.java:110)
at org.game.engine.SoundEngine.loop(SoundEngine.java:61)
at org.enginetest.EngineTest.main(EngineTest.java:30)

现在我100%肯定它不是文件它自己因为我尝试使用多个声音,赫兹高于4000,它仍然无法正常工作。 现在我知道它只接受WAV所以我只给它wav文件。 我不知道为什么这样做! BTW !!!!!重要!!!! 它不适用于高于4000 Hz的声音 只有当我尝试使用ContinuousAudioDataStream循环声音时才会这样做 如果我玩它,它完美地工作... 我该如何解决这个Hz限制问题? 也许你可以给我一个有效的代码......

这是我正在使用的两个声音文件 工作声音: http://www.mediafire.com/?kyqlr6dah71q62b 不工作的声音: http://www.mediafire.com/?72whdk7h1k7k4ee

(这里只是说它有点乱,因为把奇怪的空间放到代码盒中的东西mabob XD) 如果你需要它,我的代码是:

package org.game.engine;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.*;
import java.io.IOException;
import java.io.*;
import java.net.URL;
/**
 *
 * @author Amit
 */
public class SoundEngine {
    private String filename;
    private InputStream in;
     private AudioStream as;
     private AudioData data;
   private URL test;
     private ContinuousAudioDataStream audiostream;
     private boolean checkforlooped = false;

    public SoundEngine(String name) {
    filename = name;
    try {
        in = new FileInputStream(filename);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(SoundEngine.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        as = new AudioStream(in);
    } catch (IOException ex) {
        Logger.getLogger(SoundEngine.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public void start() {
        if (checkforlooped) {
                try {
        in = new FileInputStream(filename);
        } catch (FileNotFoundException ex) {
          Logger.getLogger(SoundEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
       try {
            as = new AudioStream(in);
        } catch (IOException ex) {
            Logger.getLogger(SoundEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    AudioPlayer.player.start(as);
    }
    public void stop() {
        AudioPlayer.player.stop(as);
    }
    public void stoploop() {
        AudioPlayer.player.stop(audiostream);
        AudioPlayer.player.stop(as);
    }
    public void loop() {
    if (!checkforlooped) {
    try {
        data = as.getData();
        audiostream = new ContinuousAudioDataStream(data);
    } catch (IOException ex) {
        Logger.getLogger(SoundEngine.class.getName()).log(Level.SEVERE, null, ex);
    }
    AudioPlayer.player.start(audiostream);
    checkforlooped = true;
    } else {AudioPlayer.player.start(audiostream);}
    }
    }

0 个答案:

没有答案