java - 单击按钮时同时播放不同的声音
问题描述
我应该如何进行正如您在此处看到的那样,三个声音同时播放 https://yadi.sk/i/eUtfS0to-0ZreQ
大家好,当我点击按钮时,声音并没有关闭,同时开始播放两三个声音。很抱歉我是在打印机翻译网站上这样做的。
public void music1(View view) {
contra=MediaPlayer.create(this,R.raw.contraolu);
contra.start();
}
public void music2(View view) {
tankurt=MediaPlayer.create(this,R.raw.tankurtmaanascenekemiklerim);
tankurt.start();
}
public void music3(View view) {
norm=MediaPlayer.create(this,R.raw.normenderciktikyineyollara);
norm.start();
}
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="44dp"
android:onClick="music2"
android:text="Tankur Manas-Çene Kemiklerimi Kırdım"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="144dp"
android:onClick="music3"
android:text="Norm Ender- Çıktık Yine Yollara"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="42dp"
android:onClick="music1"
android:text="Contra-Ölü"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
解决方案
使用单个 MediaPlayer,在开始另一个新声音之前停止它。
MediaPlayer mp;
public void music1(View view) {
stopPlayingSound();
mp=MediaPlayer.create(this,R.raw.contraolu);
mp.start();
}
public void music2(View view) {
stopPlayingSound();
mp=MediaPlayer.create(this,R.raw.tankurtmaanascenekemiklerim);
mp.start();
}
public void music3(View view) {
stopPlayingSound();
mp=MediaPlayer.create(this,R.raw.normenderciktikyineyollara);
mp.start();
}
void stopPlayingSound() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
推荐阅读
- c# - 在 Datagridview C# 中检索多个复选框
- mysql - 我如何在 AdonisJs 中获取其所有者国籍为美国人的产品?
- android - 无法在 Google 的地图信息窗口上修复宽度
- javascript - 从客户端 Javascript 签署 Streetview 静态 API 请求是否可能/值得?
- c# - DBset 为空,但 db (EF) 中有数据
- android - Volley Android JSON GET Return Code 为 200 但响应为空
- javascript - 在 javascript 中实现 OneRule 算法
- python - 我无法使用 python 3.7 代码批量安装 whl 文件
- c# - 赢表格。线程和递归
- python - 具有大量选择最佳实践的 MultipleChoiceField