如何在按下按钮时持续循环播放声音并在释放时停止?

发布于 2024-09-15 17:23:10 字数 556 浏览 6 评论 0原文

我确信我的问题非常简单。只是我不太确定如何有效地描述我想要实现的目标。

无论如何,我有一个简单的按钮,我知道如何“链接”原始声音文件以手动播放它。但我想做的可能是将 3 秒的声音链接到按钮,并在用户按住按钮时让它循环播放,然后在松开按钮时停止播放。任何人都可以分享任何示例链接,那就太好了!谢谢你!

编辑: 谢谢欧伦德上尉!

谢谢,这绝对有帮助,并且成功了,但是我遇到了 1 个小问题,也许你可以帮助我,我正在使用“自定义按钮”,如 http://developer.android.com/resources/tutorials/views/hello-formstuff.html

在我之前它工作得很好添加了“onTouch Listener。按钮现在不使用它的 android:state_pressed 项目,它只显示普通项目,而不是按下的项目。至少它现在播放声音。但是,有什么想法吗?

My question is a very simple one i'm sure. It's just I'm not quite sure how to effectively describe what i'm trying to achieve.

Anyway to the point, I have a simple button, I know how to "link" a raw sound file to hand make it play. BUT what i want to do is maybe link a 3 second sound to the button and just have it loop while the user holds the button, and then stops playing when the button is let go. Anyone have any example links they could share that would be great! Thank you!

edit:
Thank you Cpt.Ohlund!

Thanks that was definately helpful and did the trick, however i ran into 1 more small problem maybe you could help me with I'm using a "Custom Button" as seen on http://developer.android.com/resources/tutorials/views/hello-formstuff.html

It worked fine before i added the "onTouch Listener. The Button now doesn't use it's android:state_pressed Item it just displays the normal item but not the pressed item. At least It plays the sound now. But, Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-09-22 17:23:10

您可以使用以下内容,只需将 R.raw.hit 更改为您自己的声音文件:

public class XButtonSound extends Activity implements OnTouchListener {

private int sound;
private SoundPool sounds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button donePlacing = new Button(this.getApplicationContext());
    donePlacing.setId(1);
    donePlacing.setText("Play");
    donePlacing.setOnTouchListener(this);

    this.addContentView(donePlacing, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


    sounds = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    sound = sounds.load(this.getApplicationContext(), R.raw.hit, 1);
}


@Override
public boolean onTouch(View arg0, MotionEvent event) {

    switch (event.getAction() ) { 
    case MotionEvent.ACTION_DOWN: 
        System.out.println("touch");
        sounds.play(sound, 1, 1, 1, -1, 1);
        break;
    case MotionEvent.ACTION_UP:
        System.out.println("up");
        sounds.autoPause();
        break; 
    }

    return true;
}

}

You could use the following, just change R.raw.hit to your own soundfile:

public class XButtonSound extends Activity implements OnTouchListener {

private int sound;
private SoundPool sounds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button donePlacing = new Button(this.getApplicationContext());
    donePlacing.setId(1);
    donePlacing.setText("Play");
    donePlacing.setOnTouchListener(this);

    this.addContentView(donePlacing, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


    sounds = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    sound = sounds.load(this.getApplicationContext(), R.raw.hit, 1);
}


@Override
public boolean onTouch(View arg0, MotionEvent event) {

    switch (event.getAction() ) { 
    case MotionEvent.ACTION_DOWN: 
        System.out.println("touch");
        sounds.play(sound, 1, 1, 1, -1, 1);
        break;
    case MotionEvent.ACTION_UP:
        System.out.println("up");
        sounds.autoPause();
        break; 
    }

    return true;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文