【问题标题】:How can i make a sound repeatedly without having to press it multiple times (android)我怎样才能重复发出声音而不必多次按下它(android)
【发布时间】:2012-01-16 01:16:49
【问题描述】:

我在活动中有一个按钮,当我按下它时会播放声音。声音本身是 2 秒长。只有当我按下按钮时它才会播放。我想让用户可以按住按钮并播放声音,直到他释放按钮。我怎样才能做到这一点?这是我当前的代码。

package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class activity2 extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
//back button that takes u to main.xml
    Button next = (Button) findViewById(R.id.Back);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
        }
        } );
    //Button that plays sound (whippingsound)
    Button sound = (Button) findViewById(R.id.sound);
        sound.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 MediaPlayer mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);  
                 mp.start();
             }
        } );

    }   
}

谢谢!!!

【问题讨论】:

标签: java android eclipse button


【解决方案1】:

解决您的问题的方法是结合

Triggering event when Button is pressed down in Android

和

play sound while button is pressed -android.

也就是说,您使用的是 onClickListener 而不是 onTouchListener。

试试这个(注意:我还把媒体播放器移了出来,这样你就可以创建一次,然后一遍又一遍地使用它)。

    public class activity2 extends Activity{
        MediaPlayer mp = null;

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    //back button that takes u to main.xml
        Button next = (Button) findViewById(R.id.Back);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
            }
            } );
        mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);
        //Button that plays sound (whippingsound)
        Button sound = (Button) findViewById(R.id.sound);
            sound.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mp.setLooping(true);
                mp.start();
                break;

            case MotionEvent.ACTION_UP:
                mp.pause();
                break;
            }

            return true;
        }
     });
   }

【讨论】:

  • 我得到 - OnTouchListener 无法解析为类型 - View 类型中的方法 setOnTouchListener(View.OnTouchListener) 不适用于参数(new OnTouchListener(){})
  • 我更新了代码 -- 用 View.OnTouchListener 试试(OnTouchListener 接口在 View 类中定义)。
  • 谢谢,除了我得到错误语法错误,插入“}”来完成第二行分号的 ClassBody。并且底部最后一个 } 的错误表示相同的错误...有什么帮助吗?
  • 更新了正确的右括号,但如果您使用的是 Eclipse,它们会自动突出显示。如果你不是,我强烈建议你这样做——它使 Android 开发至少容易 10 倍。如果/何时对您有用,请不要忘记接受此答案。
  • 一切正常,除了声音没有停止......它只是继续前进。我可以让它在用户松开手指时停止吗?
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多