【问题标题】:How to rapidly play a sound when button is clicked(android)单击按钮时如何快速播放声音(android)
【发布时间】:2015-07-14 23:49:10
【问题描述】:

我每次都尝试播放声音,点击一个按钮。声音已播放,但我每次都需要播放(就像您键入内容时播放的声音一样),问题是,我的并不总是播放。我认为这是因为单击按钮的延迟很小。

    new MediaPlayer sound = new MediaPlayer().create(getApplicationContext(), R.raw.sound);
button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (x >= pS.getX() && x < (pS.getX() + squareS.getWidth()) &&
                y >= pS.getY() && y < (pS.getY() + squareS.getHeight())) {
            pS.setRes(pSquareOffRes);
            pS.setX(pS.getX() - squareS.getWidth());
            pS.setY(pS.getY() - squareS.getHeight());
            pS.setTouched(true);
            pS.lock(true);
            touchLimitA = System.currentTimeMillis();
            timer = 3;
            sound.start();
            break;
        }
        return false;
    }

我很高兴你能给我任何建议:)。

【问题讨论】:

    标签: java android audio android-studio


    【解决方案1】:

    如果您需要低延迟播放,您应该使用SoundPool

    【讨论】:

    • @Traviss 你也可以预加载一些声音。顺便说一句,你也可以投票给我的答案吗?
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多