多次按下按钮时如何使声音重叠?
How can I make a sound overlap when pressing a button multiple times?
我是 Android java 编程的新手,但我自己编程 Java 已经很长时间了。我创建了一个微型应用程序,它会在单击按钮时播放声音。理想情况下,单击按钮时,该按钮应播放一段声音,该声音会播放整个长度,而当声音仍在播放时单击该按钮,它将播放与当前声音重叠的相同声音。虽然,在我的应用程序中,在播放声音时按下它不会做任何事情,它只会在播放完声音后按下时播放声音。
我尝试在按下时为 sound.start()
创建一个新线程,这样它就会像我希望的那样重叠,但是在为声音创建线程时出现错误(无法解析符号 'start').
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button that is pressed to play the sound.
imgBtn = findViewById(R.id.imageViewItm);
// The sound that is played.
final MediaPlayer sound = MediaPlayer.create(this, R.raw.sound_stuff);
//Listener that will run the onClick() method when imgBtn is pressed.
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Plays sound
sound.start();
}
});
}
我认为每次单击按钮时创建一个新的 MediaPlayer
应该没问题。我还没有测试代码。如果您遇到错误,请告诉我。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button that is pressed to play the sound.
imgBtn = findViewById(R.id.imageViewItm);
//Listener that will run the onClick() method when imgBtn is pressed.
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Plays sound
MediaPlayer sound = MediaPlayer.create(MainActivity.this, R.raw.sound_stuff);
sound.start();
}
});
}
我是 Android java 编程的新手,但我自己编程 Java 已经很长时间了。我创建了一个微型应用程序,它会在单击按钮时播放声音。理想情况下,单击按钮时,该按钮应播放一段声音,该声音会播放整个长度,而当声音仍在播放时单击该按钮,它将播放与当前声音重叠的相同声音。虽然,在我的应用程序中,在播放声音时按下它不会做任何事情,它只会在播放完声音后按下时播放声音。
我尝试在按下时为 sound.start()
创建一个新线程,这样它就会像我希望的那样重叠,但是在为声音创建线程时出现错误(无法解析符号 'start').
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button that is pressed to play the sound.
imgBtn = findViewById(R.id.imageViewItm);
// The sound that is played.
final MediaPlayer sound = MediaPlayer.create(this, R.raw.sound_stuff);
//Listener that will run the onClick() method when imgBtn is pressed.
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Plays sound
sound.start();
}
});
}
我认为每次单击按钮时创建一个新的 MediaPlayer
应该没问题。我还没有测试代码。如果您遇到错误,请告诉我。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button that is pressed to play the sound.
imgBtn = findViewById(R.id.imageViewItm);
//Listener that will run the onClick() method when imgBtn is pressed.
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Plays sound
MediaPlayer sound = MediaPlayer.create(MainActivity.this, R.raw.sound_stuff);
sound.start();
}
});
}