English 中文(简体)
怎么能够反复地发出声音,而不必多次施加压力(安德罗德)。
原标题:How can i make a sound repeatedly without having to press it multiple times (android)

我在一项活动中有一席之地,当我向它发出声音时。 声音本身长2秒。 它只在 but子上发挥作用。 我的大麻使使用者能够控制 but子和声音,直到他释放ton子为止。 如何做到这一点? 我目前的法典就是这样。

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();
             }
        } );

    }   
}

Thanks!!!

最佳回答

解决你的问题,是综合的。

Button在Anthony/a受到压制时的触发事件

and

play sound while button is pressed -android.

也就是说,你在ClickListener,而不是在TouchListener使用。

相反(注:我也把媒体角色引出,以便一劳永逸地创建媒体。)

    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;
        }
     });
   }
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签