【发布时间】:2015-09-23 14:49:32
【问题描述】:
我有一个 ImageView。当我触摸它时,颜色会发生变化,并且检查器的声音开始播放。当我从 ImageView 上移开手指时,颜色再次改变,声音应该停止播放。
一切正常,但声音不会停止。我尝试将 stop() 方法放入 OnClickListener 中,但没有成功。
当我从 ImageView 移开手指时如何停止声音?
btn_censor.setOnTouchListener(new OnTouchListener {
@override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
btn_censor.setImageResource(R.drawable.btn_censor_focus);//Change color of the ImageView
//Parameters for sound
float vol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float leftVolume = vol / maxVol;
float rightVolume = vol / maxVol;
int priority = 1;
int loop = -1; //infinite loop
float normal_plaback_rate = 1f;
soundPool.play(censor_ID, leftVolume, rightVolume, priority, loop, normal_playback_rate);//Play the sound
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btn_censor.setImageResource(R.drawable.btn_censor);//Change colour of the ImageView
soundPool.stop(censor_ID);//This should stop the sound
}
return false;
};
};
【问题讨论】:
标签: android audio android-studio ontouchlistener soundpool