单击按钮的同时播放不同的声音

时间:2019-08-06 07:07:09

标签: java android android-mediaplayer

我应该如何进行     如您所见,三种声音同时播放     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" />

2 个答案:

答案 0 :(得分:1)

使用一个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;
           }
    }

答案 1 :(得分:0)

在播放下一首音乐之前,您必须停止播放器。我建议您创建一个名为mediaPlayer的公共字段,然后可以在每种方法中将其停止并使用它播放新音乐。

相关问题