歌曲播放第一次,但没有发挥一度停止:夹在Java中 [英] Song plays first time but does not play once stopped : Clip in Java

查看:208
本文介绍了歌曲播放第一次,但没有发挥一度停止:夹在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用夹在java中播放一首歌曲如下:

I am using Clip in java to play a song as follows:

        MR.clip= (Clip) AudioSystem.getLine(MR.info[docIdOfSelectedSong]);
        MR.clip.open(MR.sounds[docIdOfSelectedSong]);
        MR.clip.setMicrosecondPosition(5* 1000000);
        MR.clip.start();

其中MR.sounds是一个类型的数组的AudioInputStream 和MR.info是类型的数组 DataLine.info 。当我preSS一个按钮,上面的code被称为播放的歌曲。此外,我还有一个按钮来停止它调用下面的code中的歌曲

where MR.sounds is an array of type AudioInputStream and MR.info is an array of type DataLine.info. When I press a button ,the above code is called to play the song. Moreover, I have another button to stop the song which calls the below code

public static void stopSong(){

    MR.clip.close();

}

问题是,当我播放的歌曲,第一次,播放和停止按钮工作正常。但是,当我尝试播放歌曲第二次,我不能听的歌曲。关于什么错误有什么建议?

The problem is that when I play the song for the first time, the play and stop button are working fine. But, when I try to play the song for the second time, I cannot hear the song. Any suggestions on what is going wrong?

推荐答案

像所有其他InputStreams,的AudioInputStream只能读取一次(除非能够的。reset()的)。你可以尝试在调用的AudioInputStream .reset段()attemping再次播放声音之前,但的AudioInputStream可能不支持.reset段()。 InputStreams不需要支持复位。另请参见 markSupported()

Like all other InputStreams, AudioInputStream can only be read once (unless it can be .reset()). You could try calling .reset() on the AudioInputStream before attemping to play the sound again, but AudioInputStream may not support .reset(). InputStreams are not required to support reset. Also see markSupported().

如果.reset段()不工作,考虑每一个你需要开始播放的时间建设一个新的AudioInputStream。

If .reset() doesn't work, consider constructing a new AudioInputStream every time you need to start playing.

更新:我在做内存缓存的声音数据,并使用剪辑播放这些声音的一个例子。这个示例使用AudioInputStream.reset()。所以,这怎么行?事实上,的AudioInputStream的确实的支持复位()的当且仅当的其底层InputStream支持.reset段()。所以,我的例子创建了由一个ByteArrayInputStream支持的AudioInputStream。由于ByteArrayInputStream的支持复位,这些缓存AudioInputStreams也支持.reset段(),这使得他们可以重复使用。

UPDATE: I made an example of caching sound data in memory and using Clip to play those sounds. This example utilizes AudioInputStream.reset(). So how can that work? In fact, AudioInputStream does supports reset() if and only if its underlying InputStream supports .reset(). So my example creates an AudioInputStream that is backed by a ByteArrayInputStream. Because ByteArrayInputStream supports reset, these cached AudioInputStreams also support .reset(), which allows them to be reused.

请注意,如果你将要扮演任何一个缓存的声音的同时,你应该不缓存的AudioInputStream S,而是缓存字节[ ] 和每播放构建的AudioInputStream 。这是因为的AudioInputStream 是有状态的,所以通过它的单个实例两个同时运行的剪辑,或重置当一个剪辑播放的流,将导致国家冲突。

Note that if you are going to be playing any one cached sound concurrently, you should probably not cache AudioInputStreams, but rather cache byte[]s and construct an AudioInputStream per-playback. This is because AudioInputStream is stateful, so passing a single instance of it to two concurrently running clips, or resetting a stream while one clip is playing, will result in state conflict.

public class CachedSoundClipTest
{
    static ArrayList<AudioInputStream> cachedSounds = 
        new ArrayList<AudioInputStream>();

    public static void main(String[] args) throws Exception
    {
        File[] audioFiles = new File("/audio_storage_directory").listFiles();
        for (File file : audioFiles)
        {
            AudioInputStream reusableAudioInputStream = 
                createReusableAudioInputStream(file);
            cachedSounds.add(reusableAudioInputStream);
        }

        while(true)
        {
            System.out.println("Press enter to play next clip");
            BufferedReader br = 
                new BufferedReader(new InputStreamReader(System.in));
            br.readLine();
            playCachedSound(0);
        }
    }

    private static void playCachedSound(int i) 
        throws IOException, LineUnavailableException
    {
        AudioInputStream stream = cachedSounds.get(i);
        stream.reset();
        Clip clip = AudioSystem.getClip();
        clip.open(stream);
        clip.start();
    }

    private static AudioInputStream createReusableAudioInputStream(File file) 
        throws IOException, UnsupportedAudioFileException
    {
        AudioInputStream ais = null;
        try
        {
            ais = AudioSystem.getAudioInputStream(file);
            byte[] buffer = new byte[1024 * 32];
            int read = 0;
            ByteArrayOutputStream baos = 
                new ByteArrayOutputStream(buffer.length);
            while ((read = ais.read(buffer, 0, buffer.length)) != -1)
            {
                baos.write(buffer, 0, read);
            }
            AudioInputStream reusableAis = 
                new AudioInputStream(
                        new ByteArrayInputStream(baos.toByteArray()),
                        ais.getFormat(),
                        AudioSystem.NOT_SPECIFIED);
            return reusableAis;
        }
        finally
        {
            if (ais != null)
            {
                ais.close();
            }
        }
    }
}

这篇关于歌曲播放第一次,但没有发挥一度停止:夹在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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