我的代码仅在 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"
/>
最佳答案
您应该使用如下所示的内容
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;
}
});
关于java - 如何在按下 ImageView 时立即启动音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830128/