Reputation: 39
How should I proceed As you can see here, three sounds play at the same time https://yadi.sk/i/eUtfS0to-0ZreQ
Hello everyone, When I click the button, the sound does not turn off and two or three sounds start playing at the same time. I'm sorry I did this on the printer translation site.
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" />
Upvotes: 2
Views: 87
Reputation: 76
Use a single MediaPlayer, stop it before starting another new sound.
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;
}
}
Upvotes: 1
Reputation: 756
You have to stop the player before playing you next music. I suggest you create a public field named mediaPlayer
and then you can stop it in each method and play new music with it.
Upvotes: 0