【发布时间】: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