我的代码仅在释放ImageView时播放音频。我应该添加什么才能在按下音频后立即启动音频?
MediaPlayer mp = null;
public void next (View view) {
if (mp != null) {
mp.stop();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.dry);
mp.start();
}
XML代码:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/seethrough"
android:clickable="true"
android:id="@+id/Seethrough"
android:onClick="next"
/>
答案 0 :(得分:1)
您应该使用以下内容
ImageView seethrough= (ImageView)this.findViewById(R.id.Seethrough);
seethrough.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
// this is where you should call next()
else if (event.getAction() == MotionEvent.ACTION_UP)
// this is if you are interested in the release action
return false;
}
});
答案 1 :(得分:0)
复制并粘贴它,它应该正常工作!
ImageView seethrough;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
seethrough = (ImageView) findViewById(R.id.Seethrough);
seethrough.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mp != null) {
mp.stop();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.dry);
mp.start();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}