我正在使用import pytest
import random
d = {1: 'Hi',2: 'How',3: 'Are',4:'You ?'}
def setup_function(function):
print("setUp",flush=True)
def teardown_function(functions):
print("tearDown",flush=True)
@pytest.mark.parametrize("test_input", [1,2,3,4])
def test_one(test_input):
print("Test with data " + str(test_input))
print(d[test_input])
assert True
播放声音MediaPlayer
。在声音结束之前,点击事件不再播放声音。当声音正在播放时,如何在点击时再次播放声音?
onClick
答案 0 :(得分:0)
您必须创建一个新的MediaPlayer
对象才能再次播放声音。
tv_mistake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mistake != null) {
mistake.release();
mistake = MediaPlayer.create(getActivity(), R.raw.mistake);
}
mistake.start();
}
});
您可以在以下链接中阅读有关MediaPlayer的更多信息。
也非常相似question
MediaPlayer,MediaPlayer Tutorial,MediaPlayer Tutorial From Google
答案 1 :(得分:0)
MediaPlayer
中的大多数错误都是由于对其对象的states的不正确处理造成的。
完成播放后或再次致电MediaPlayer
之前,您应该释放start()
个对象。
还建议一旦不再使用MediaPlayer对象,立即调用release(),以便可以立即释放与MediaPlayer对象关联的内部播放器引擎使用的资源。
创建MediaPlayer
对象:
Mediaplayer mediaPlayer = null;
点击按钮时调用playMistakeSound()
:
tv_mistake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMistakeSound()
}
});
将playMistakeSound()
实施为:
void playMistakeSound() {
try {
// releases MediaPlayer object before calling create() again while previous is still playing
if (mediaPlayer != null){
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(getActivity() /*Context*/, R.raw.mistake);
// this will release MediaPlayer as soon as it completes
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mp != null) {
mp.reset();
mp.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
} catch (Exception e) {
// log exception and handle
e.printStackTrace();
}
}