自动启用按钮 [英] Automatically enable button

查看:89
本文介绍了自动启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个演示WAV文件的演示并坚持下去。当我开始播放音乐时,我禁用了btnPlay。现在我想要在音乐播放完毕后,必须自动启用btnPlay。但我不能这样做。我可以用微秒获得文件持续时间,但我不知道接下来该做什么。这是代码。



I'm making a demo to play WAV file and stuck with this. When I begin to play the music, I disable the btnPlay. Now I want when the music finish playing, the btnPlay must be enable automatically. But I can't do this. I can get the file duration in microsecond but I don't know what to do next. Here is the code.

public void playmusic(){
        try{
            btnClose.setEnabled(true);
            btnShuffle.setEnabled(true);
            btnRepeat.setEnabled(true);
            btnPause.setEnabled(true);
            btnStop.setEnabled(true);
            if(isPausing==false){
                AudioInputStream ais = AudioSystem.getAudioInputStream(f);
                clip = AudioSystem.getClip();
                clip.open(ais);
                clip.start();
            }
            else{
                isPausing=false;
                clip.start();
            }
        }
        catch(Exception ex){}
    }





你能给我一个解决方案吗?非常感谢!



Could you give me a solution for this? Thank you very much!

推荐答案

您似乎需要订阅一个活动。剪辑对象似乎有一个completed(),DonePlaying(),Finished()或一些在剪辑完成时会触发的事件。在这种情况下,它似乎可能启用你的播放按钮,但你也想要禁用暂停并停止你从上面开始。



制作一个新的方法控制你的按钮 - 传入bool,如果是否正在播放,然后在那里切换你的按钮,在上面的例程中调用它,并在你要订阅的新事件处理程序中。
It would appear that you need to subscribe to an event. It would seem likely that the clip object has a completed(), DonePlaying(), Finished() or some such event that would fire when the clip is completed. In this event, it would seem likely to enable your play button, but you'll also want to disable the pause and stop you started above.

Make a new method to control your buttons too - pass in bool that says if playing or not, and then toggle your buttons there, calling it in the above routine, and in the new event handler you're going to subscribe to.


检查观察者模式

按钮是观察者,音频蒸汽是可观察的主题。音频完成后,您可以发出notify()。



要检查音频的状态,您需要一个线程来检查(即大小的流式字节或类似的东西) - 如果流对象是预先的-implemented。
Check Observer Pattern.
the button is the observer and audio steam is observable subject. you can raise an notify(), once your audio is complete.

To check the status of your audio, you will need a thread to check (ie. the sized of streamed bytes or something similar) - if the stream object is pre-implemented.


我找到了方法。只需添加一个LineListener,一切正常......:D





I found the way. Just add a LineListener and everything works fine... :D


public void playmusic(){
        try{
            btnClose.setEnabled(true);
            btnShuffle.setEnabled(true);
            btnRepeat.setEnabled(true);
            btnPause.setEnabled(true);
            btnStop.setEnabled(true);
            LineListener listener = new LineListener() {
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP) {
                        btnPlay.setEnabled(true);
                    }        
                }
            };
            
            if(isPausing==false){
                AudioInputStream ais = AudioSystem.getAudioInputStream(f);
                clip = AudioSystem.getClip();
                clip.open(ais);
                clip.start();
            }
            else{
                isPausing=false;
                clip.start();
            }
            clip.addLineListener(listener);
        }
        catch(Exception ex){}
    }


这篇关于自动启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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